我正在创建一个简单的小费计算器来学习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];
在我的if statement
中的updateValues()
内创建TipViewController.m
以加载默认值。
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];
}
我不确定我是否正确这样做,但这是我到目前为止所尝试过的。
答案 0 :(得分:1)
rdelmar评论是正确的,除非您确实希望在应用程序的启动之间保持该值。 (在这种情况下,我会这么认为。) 移动线
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int intValue = [defaults integerForKey:@"another_key_that_you_choose"];
在updateValues
内,因此每次调用intValue
时updateValues
的值都是最新的。