首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解除modalViewController的modalViewController

解除modalViewController的modalViewController
EN

Stack Overflow用户
提问于 2011-09-17 00:54:35
回答 3查看 9.3K关注 0票数 1

所以我有一个UITabBarController应用程序,我想要显示一个登录页面,所以我做到了:

代码语言:javascript
复制
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:UserDidLoginNotification object:nil];
LoginViewController* loginViewController = [[LoginViewController alloc] init];
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
        [self.tabBarController.selectedViewController presentModalViewController:loginViewController animated:NO];
        [loginViewController release];

在我的LoginViewController中,我也可以显示另一个modalViewController:

代码语言:javascript
复制
- (void) twitterLogin: (UIViewController *) askingView
{
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self];

    if (controller) {
        self.askingView = askingView;
        [askingView presentModalViewController: controller animated: YES];
    }
}

我有以下方法,其中askingView是LoginViewController,当我想要忽略它时,我会这样做:

代码语言:javascript
复制
[self.askingView dismissModalViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil];

但是,这并不会忽略LoginViewController并显示UITabBarController视图。它只是关闭我在LoginvVIewController中显示的modalViewController。我在这里做错了什么?我还收到以下错误:

代码语言:javascript
复制
attempt to dismiss modal view controller whose view does not currently appear. self = <LoginViewController: 0x2aff70> modalViewController = <SA_OAuthTwitterController: 0x2d2a80>
2011-09-16 09:45:37.750 VoteBooth[4614:707] attempt to dismiss modal view controller whose view does not currently appear. self = <MainViewController: 0x29fec0> modalViewController = <LoginViewController: 0x2aff70>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-17 01:13:59

为了消除在另一个模式视图上呈现的模式视图,您必须在父视图的父视图上调用dismissModalViewControllerAnimated:。我已经在我的一些应用程序中使用了它,它对我来说工作得很好(我花了很多时间努力弄清楚它)。下面就是我用过的代码:

代码语言:javascript
复制
[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
票数 13
EN

Stack Overflow用户

发布于 2011-10-26 12:40:42

代码语言:javascript
复制
if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+
} else {
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5
}
票数 8
EN

Stack Overflow用户

发布于 2012-12-31 22:38:25

如果你有一个动态的UX,并且不知道要去多少个父级,你可以使用这个递归函数来解决这个问题。

代码语言:javascript
复制
- (void) goHome
{
    //Dismiss modal back to home page
    int numberOfVcsToDismiss = [self findRootViewController:self];
    [self dismissToRootVc:numberOfVcsToDismiss];
}

- (int) findRootViewController:(UIViewController*)vc
{
    if(vc)
    {
        return 1 + [self findRootViewController:vc.presentingViewController];
    }
    return 0;
}

- (void) dismissToRootVc:(int)count
{
    if(count == 1)
        [self dismissViewControllerAnimated:YES completion:^{}];
    if(count == 2)
        [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 3)
        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 4)
        [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 5)
        [self.presentingViewController.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    //etc....
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7448013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档