在viewdidload方法中切换视图控制器

时间:2011-07-13 05:36:40

标签: iphone objective-c nsuserdefaults viewcontroller

我正在开发一个iphone应用程序,因为我想通过检查userdefaults来切换视图控制器。所以我做了以下事情:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults objectForKey:@"keyString"] != nil) {
    nextViewController *redirectView = [[nextViewController alloc] initWithNibName:@"nextViewController" bundle:[NSBundle mainBundle]];
self.nextView = redirectView;
[self presentModalViewController:self.nextView animated:YES];
} else {
[defaults setObject:@"newValue" forKey:@"keyString"];
}

它不起作用..这是正确的方法???如果不是,任何人都可以告诉我该怎么做..

3 个答案:

答案 0 :(得分:1)

不要将redirectView分配给self.nextView。像这样直接传递

[self presentModalViewController:redirectView animated:YES];

答案 1 :(得分:1)

添加行

[self.view addSubview:nextView]; 

而不是

[self presentModalViewController:self.nextView animated:YES];

答案 2 :(得分:0)

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults objectForKey:@"keyString"] != nil) 
{ nextViewController *redirectView = [[nextViewController alloc]init]; 
[self presentModalViewController:redirectView animated:YES]; 
redirectView=nil;
 } 
else { 
[defaults setObject:@"newValue" forKey:@"keyString"]; 

}