2010-07-07 89 views
0

我是iPhone編程新手。在我的應用程序中,我有HomeViewController和ContentViewController。我保存在ContentViewController中的值通過使用NSUserDefaults 並基於保存的值我將加載ContentView而不是HomeView時,應用程序重新啓動。如果在NSUserDefaut中沒有任何值,則顯示HomeView。問題viewDidLoad ..我可以在viewDidLoad中調用presentModelViewController ..?

在HomeView我有一些按鈕..像這樣..每個按鈕都是爲了一本書,所以在contentView所有頁面(在ContentView的滾動視圖的底部)將顯示,如果我點擊一個頁面不,它顯示ContentView的上述標籤中的文本。如果用戶在contentView中關閉應用程序,頁面號和書號將被保存...如果用戶單擊主頁按鈕,所有信息將被刪除。 在Homeview IM檢查NSUserDefaults的,如果它包含的值應該顯示的那本書 的,準確的頁面下面是代碼...

//HomeViewController.m

  • (無效)viewDidLoad中{

[super viewDidLoad];

contentViewController = [[ContentViewController alloc] initWithNibName:@「ContentView」bundle:nil];

NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; (「@%d,%d,%d」,[prefs integerForKey:@「Stage」],[prefs integerForKey:@「Stimulus」],[prefs integerForKey:@「Card」] );

IF(([首選項integerForKey:@ 「舞臺」] == 0 & & [首選項integerForKey:@ 「刺激」] == 0 & & [首選項integerForKey:@ 「一卡通」] == 0)) {content_controller setCurrentState:[prefs integerForKey:@「Stage」]];

[contentViewController setCurrentStimulus:[prefs integerForKey:@「Stimulus」]];

[contentViewController setCurrentCard:[prefs integerForKey:@「Card」]];

[self presentModalViewController:contentViewController animated:YES];

} 

}

但其顯示homeview ...

任何人可以幫我請.......

回答

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]; 
    } 
} 
+0

謝謝你的工作...... – rockey 2010-07-07 21:25:31