我是iPhone编程新手。在我的应用程序中,我有HomeViewController和ContentViewController。我正在使用NSUserDefaults保存ContentViewController中的值 并且基于保存的值,我将在重新启动应用程序时加载ContentView而不是HomeView。如果NSUserDefautls中没有值,则显示HomeView。
在HomeView中我有一些按钮..就像这样..每个按钮都是一本书所以在contentView中,如果我点击一个页面,将显示所有页面nos(在ContentView的滚动视图的底部)它在ContentView的上述标签中显示文本。如果用户在contentView中关闭应用程序,则页面号和书号将被保存...如果用户点击主页按钮,则所有信息都将被删除。 在Homeview中我检查NSUserDefaults,如果它包含值,它应该显示该书的确切页面 以下是代码......// HomeViewController.m
[super viewDidLoad];
contentViewController = [[ContentViewController alloc] initWithNibName:@“ContentView”bundle:nil];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
NSLog(@“...%d,%d,%d”,[prefs integerForKey:@“Stage”],[prefs integerForKey:@“Stimulus”],[prefs integerForKey:@“Card”]) ;
if(!([prefs integerForKey:@“Stage”] == 0&& [prefs integerForKey:@“Stimulus”] == 0&& [prefs integerForKey:@“Card”] == 0)){
[contentViewController setCurrentState:[prefs integerForKey:@“Stage”]];
[contentViewController setCurrentStimulus:[prefs integerForKey:@“Stimulus”]];
[contentViewController setCurrentCard:[prefs integerForKey:@“Card”]];
[self presentModalViewController:contentViewController animated:YES];
}
}
但它显示了主页...
请有人帮帮我.......
答案 0 :(得分:1)
尝试使用下面显示的方法 viewDidAppear ,而不是 viewDidLoad
- (void)viewDidAppear:(BOOL)animated
{
contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(@"...%d,%d,%d",[prefs integerForKey:@"Stage"],[prefs integerForKey:@"Stimulus"], [prefs integerForKey:@"Card"]);
if(!([prefs integerForKey:@"Stage"] ==0 && [prefs integerForKey:@"Stimulus"] ==0 && [prefs integerForKey:@"Card"] ==0))
{
[contentViewController setCurrentState:[prefs integerForKey:@"Stage"]];
[contentViewController setCurrentStimulus:[prefs integerForKey:@"Stimulus"]];
[contentViewController setCurrentCard:[prefs integerForKey:@"Card"]];
[self presentModalViewController:contentViewController animated:YES];
}
}