2013-12-15 63 views
0

我正在創建一個簡單的提示計算器,以瞭解iOS開發的基礎知識,並且無法從一個頁面保存值並在另一個頁面中重複使用。保存並使用值提示計算器應用程序

目前,我的根頁(又名TipViewController.m):

#import "TipViewController.h" 
#import "SettingsViewController.h" 

@interface TipViewController() 

@property (weak, nonatomic) IBOutlet UITextField *billTextField; 
@property (weak, nonatomic) IBOutlet UILabel *tipLabel; 
@property (weak, nonatomic) IBOutlet UILabel *totalLabel; 
@property (weak, nonatomic) IBOutlet UISegmentedControl *tipControl; 

- (IBAction)onTap:(id)sender; 
- (void)updateValues; 
- (void)onSettingsButton; 

@end 

@implementation TipViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = @"Tip Calculator"; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self updateValues]; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(onSettingsButton)]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)onTap:(id)sender { 
    [self.view endEditing:YES]; //keyboard goes away 
    [self updateValues]; 
} 

- (void)updateValues{ 
    float billAmount = [self.billTextField.text floatValue]; 

    // array to hold all tip values 
    NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)]; 
    float tipAmount = billAmount*[tipValues[self.tipControl.selectedSegmentIndex] floatValue]; 
    float totalAmount = tipAmount + billAmount; 

    self.tipLabel.text = [NSString stringWithFormat:@"$%0.2f", tipAmount]; 
    self.totalLabel.text = [NSString stringWithFormat:@"$%0.2f", totalAmount]; 
} 

- (void)onSettingsButton { 
    [self.navigationController pushViewController:[[SettingsViewController alloc] init] animated:YES]; 
} 

@end 

當我輕觸右上角的設置按鈕,它會導致我到我的設置頁面:

#import "SettingsViewController.h" 

@interface SettingsViewController() 
@property (weak, nonatomic) IBOutlet UITextField *defaultTipPercentage; 

- (IBAction)ontap:(id)sender; 

@end 

@implementation SettingsViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = @"Settings"; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (IBAction)ontap:(id)sender { 

    [self.view endEditing:YES]; 
} 
@end 

眼下我希望將TextField中的值作爲返回到根頁面時使用的默認提示百分比。

我想將這一ontap()下保存的值(SettingsViewController.m)的:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:@"some_string_to_save" forKey:@"some_key_that_you_choose"]; 
[defaults setInteger:123 forKey:@"another_key_that_you_choose"]; 
[defaults synchronize]; 

,並在我的TipViewController.m裏面創建一個updateValues()if statement加載默認值。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *stringValue = [defaults objectForKey:@"some_key_that_you_choose"]; 
int intValue = [defaults integerForKey:@"another_key_that_you_choose"]; 

- (void)updateValues{ 
    float billAmount = [self.billTextField.text floatValue]; 

    if (intValue) { 
     float tipAmount = .01 * intValue; 
    }else { 

    // array to hold all tip values 
    NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)]; 
    float tipAmount = billAmount*[tipValues[self.tipControl.selectedSegmentIndex] floatValue]; 
    float totalAmount = tipAmount + billAmount; 
    } 

    self.tipLabel.text = [NSString stringWithFormat:@"$%0.2f", tipAmount]; 
    self.totalLabel.text = [NSString stringWithFormat:@"$%0.2f", totalAmount]; 
} 

不知道我是否正確地做了這件事,但這是我迄今爲止嘗試過的。

+1

這不是NSUserDefaults的一個好用處 - 它們用於在應用程序啓動之間保持值,而不是用於在控制器之間傳遞數據。您應搜索此站點上的「在控制器之間傳遞數據」。這個主題有成千上萬的問題和答案。 – rdelmar

回答

1

rdelmar評論是正確的,除非你想保持應用程序啓動之間的價值。 (在這種情況下,我會這樣想。) 移動線

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
int intValue = [defaults integerForKey:@"another_key_that_you_choose"]; 

updateValues裏面,這樣的intValue該值當前每次updateValues被調用。