2013-12-14 73 views
0

我想編程一個頁面,以便當我點擊'返回'按鈕時,我得到了最後訪問的UIViewController。檢測最後視圖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

你需要從類實例化一個objcet,就像這樣:

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: ! 
+0

當我按下它,它漸黑。我正在使用一個故事板順便說一句。 –

+0

@SeOH在這種情況下,你不應該保存視圖控制器的類,而是保存一個故事板ID。然後使用'instantiateViewControllerWithIdentifier:'來創建你的視圖。 –