浏览View Controllers的正确方法

时间:2012-06-26 15:14:04

标签: ios uiviewcontroller

我正在为iPad制作电子书。本书的每一页都是应用程序StoryBoard中的View Controller,其页码在Interface Builder中设置为其标识符。我正在研究实现导航的类,其作用是呈现和关闭视图控制器。它通过在呈现的视图控制器上叠加一组按钮并处理它们的IBActions来实现。

我的问题是一些视图控制器(偶数页面)永远不会出现!谁能明白为什么?

@implementation Navigator

UIViewController *currentPageVC;
UIViewController *nextPageVC;

int currentPage = 0;

-(void)viewDidLoad{
    [self getLandingPage];
}

-(void)getLandingPage{

    currentPageVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Landing"];
    currentPage = 0;
    NSLog(@"Trying to present landing page");
}

-(IBAction)goToNextPage{

    [currentPageVC dismissModalViewControllerAnimated:YES];
    currentPage++;    

    nextPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%d", currentPage ]];
    [self presentModalViewController:nextPageVC animated:YES];

    [nextPageVC.view addSubview:self.view];

    currentPageVC = nextPageVC;

    NSLog(@"Current Page is %d", currentPage);
}

1 个答案:

答案 0 :(得分:0)

好的,修好了。之所以发生这种情况,是因为presentModalViewController:

之后很快就调用了dismissModalViewControllerAnimated:

要解决此问题,您必须使用新版本:dismissViewControllerAnimated:completion:

-(IBAction)goToNextPage{    
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"dismissed!");
        currentPage++;
        UIViewController *nextPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%d", currentPage ]];
        [self presentModalViewController:nextPageVC animated:YES];
        [nextPageVC.view addSubview:self.view];

        NSLog(@"Current Page is %d", currentPage);

    }];

}