检测上次查看UIViewController

时间:2013-12-14 01:51:03

标签: uiviewcontroller ios7 nsuserdefaults

我正在尝试对页面进行编程,以便当我单击“返回”按钮时,我获得了上次访问的UIViewController。

最后一页代码:

[[NSUserDefaults standardUserDefaults] setObject:NSStringFromClass([self class]) forKey:@"currentViewController"];
[[NSUserDefaults standardUserDefaults] synchronize]; 

返回按钮代码:

    - (IBAction)Return:(id)sender {
        NSString *savedClassName = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentViewController"];
UIViewController *screen = (UIViewController *)NSClassFromString(savedClassName);
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
            }

问题是它不断返回错误:

+[SAVEDVIEWNAME setModalTransitionStyle:]: unrecognized selector sent to class 0x59b98
2013-12-14 12:47:27.462 Friends+[16358:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Calander setModalTransitionStyle:]: unrecognized selector sent to class 0x59b98'

我理解它想说的是什么,但它没有意义,因为在这种情况下完全可以接受!!!

任何帮助表示感谢,thnx。

1 个答案:

答案 0 :(得分:1)

您需要从类中实例化一个对象,如下所示:

NSString *savedClassName = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentViewController"];
Class cls = NSClassFromString(savedClassName);
UIViewController *screen = (UIViewController *)[cls new];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:screen animated:YES completion:nil]; //You should use this instead of presentModalViewController:animated: !