ios presentModalViewController具有两个视图

ios presentModalViewController具有两个视图,ios,view,modalviewcontroller,Ios,View,Modalviewcontroller,我很困惑。我有一个导航控制器和一个打开第一视图的气压表。完成一些工作后,我希望此视图消失,并打开第二个视图 根视图:导航控制器 第一个视图:活动指示器,其中一些数据放在一起 第二个视图:MFMailComposeViewController 在根视图中,BarItem运行以下行以打开第一个视图: IndicatorViewController *indicator = [[IndicatorViewController alloc] initWithNibName:@"Indicator

我很困惑。我有一个导航控制器和一个打开第一视图的气压表。完成一些工作后,我希望此视图消失,并打开第二个视图

根视图:导航控制器 第一个视图:活动指示器,其中一些数据放在一起 第二个视图:MFMailComposeViewController 在根视图中,BarItem运行以下行以打开第一个视图:

    IndicatorViewController *indicator = [[IndicatorViewController alloc] initWithNibName:@"IndicatorViewController" bundle:nil];

    indicator.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentModalViewController:indicator animated:YES];
- (void) viewDidAppear:(BOOL)animated {
    static BOOL firstTime = YES;
    if (firstTime) {
        //do stuff that takes some time (that's why I show the indicator)
        MailViewController *controller = [[MailViewController alloc] init];

        if (controller) 
           [self presentModalViewController:controller animated:YES]; 
        firstTime = NO;
    } else {
        [self dismissModalViewControllerAnimated:YES];
    }    
}
第一个视图指示器VIEWCONTROLLER执行一些工作并最终运行

[self dismissModalViewControllerAnimated:YES];
这个很好用。但是-如何打开第二个视图

我试过这个:

我打开第二个视图。在关闭第二个视图后,我的第一个视图再次弹出,因为它仍然在那里,并且在这一点上被忽略。此代码放置在第一个视图中:

    IndicatorViewController *indicator = [[IndicatorViewController alloc] initWithNibName:@"IndicatorViewController" bundle:nil];

    indicator.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentModalViewController:indicator animated:YES];
- (void) viewDidAppear:(BOOL)animated {
    static BOOL firstTime = YES;
    if (firstTime) {
        //do stuff that takes some time (that's why I show the indicator)
        MailViewController *controller = [[MailViewController alloc] init];

        if (controller) 
           [self presentModalViewController:controller animated:YES]; 
        firstTime = NO;
    } else {
        [self dismissModalViewControllerAnimated:YES];
    }    
}
由于第一个视图再次弹出,在第二个视图关闭后,用户可以再次看到指示器——这不是我想要的


我错过了什么?做这件事最好的方法是什么?

我会这样做。创建一个navigationController,并将第一个视图作为根控制器。然后像这样做:

FirstView.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void) nextView { // however you get to your next view, button/action/etc.
    UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
    [self.navigationController pushViewController:screen animated:YES];
}
然后在第二个视图中:

SecondView.m
- (void) nextView { // however you get to your next view, button/action/etc.
    UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
    [self.navigationController pushViewController:screen animated:YES];
}
最后在rootview中:

RootView.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *navStack = [NSArray arrayWithObject:self];
    self.navigationController.viewControllers = navStack;
    [self.navigationController setNavigationBarHidden:NO];
}
这将使您的RootView成为NavigationController的新RootView

self.navigationController.ViewController


是包含NavController堆栈上所有ViewController的阵列。第一个对象是rootcontroller。如果我们用自己的数组替换整个数组,它只知道一项。如果这是你想要的,你可以解雇。这不是最漂亮的方法,但也不是最坏的方法。

更好的方法是使用navigationcontroller,并使用[self.navigationcontroller pushViewController:YourViewController]@Martol1ni我希望第一视图和第二视图使用全屏,我不希望它们成为navigationcontroller的一部分,这就是我使用的原因[自我呈现ModalViewController]他们仍然可以在使用navigationcontroller时使用全屏。是否要从第一个视图开始,转到第二个视图,然后从根视图启动navigationcontroller?如果您更好地解释视图层次结构,就更容易提供帮助:是的,这正是我想要的:-您是否打算返回到第一个视图从第二个视图?还是总是第一个视图>第二个视图>启动navigationcontroller的堆栈