开发者

Can I call presentModalViewController in viewDidLoad..?

In my iPhone app I have HomeViewController and ContentViewController. I am saving the values in ContentViewController by using NSUserDefaults and based on saved values I will load the ContentView instead of HomeView when the app is restarted. if there r no values in the NSUserDefaults it displays the HomeView.

in HomeView I have some buttons..it's like this.. each button is for a book so in contentView all the page nos (in the bottom in a scroll view in ContentView) will be displayed if I click on a page no it displays the text in the above label of ContentView.if the user closes the app in contentView, the page no and book no will be saved...if the user clicks on home button all the information will be deleted. In the Homeview im checking the NSUserDefaults, if it contains values it should display that exact page of that book the following is the code...

//HomeViewController.m

- (void)viewDidLoad {

    [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];
    }
}

but it's displaying the homeview.


Try using the method viewDidAppear shown below instead of 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜