解除 modalViewController 的 modalViewController

发布于 2024-12-04 20:46:00 字数 1817 浏览 0 评论 0原文

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

 [[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:

- (void) twitterLogin: (UIViewController *) askingView
{
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self];

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

我有以下方法,其中 AskView 是 LoginViewController, 当我想关闭它时,我这样做:

[self.askingView dismissModalViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil];

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

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>

So I have a UITabBarController app and I want to display a login page, and so I did:

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

Inside my LoginViewController I can as well show another modalViewController:

- (void) twitterLogin: (UIViewController *) askingView
{
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self];

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

I have the following method where the askingView is the LoginViewController,
when I want to dismiss this I do:

[self.askingView dismissModalViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil];

However, this doesn't dismiss the LoginViewController and show the UITabBarController views.. it just dismisses my modalViewController shown from the LoginvVIewController. What am I doing wrong here? I am also getting the following error:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦境 2024-12-11 20:46:00

为了关闭在另一个模态视图上呈现的模态视图,您必须在父级的父级上调用 dismissModalViewControllerAnimated: 。我已经在我的一些应用程序中使用了它,并且它对我来说非常有效(经过许多艰苦的时间试图弄清楚)。这正是我所使用的:

[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];

In order to dismiss a modal view that is presented over another modal view, you have to call dismissModalViewControllerAnimated: on the parent of the parent. I have used this in some of my apps and it has worked beautifully for me (after many painstaking hours trying to figure it out). Here is exactly what I've used:

[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
清醇 2024-12-11 20:46:00
if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+
} else {
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5
}
if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+
} else {
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5
}
忘你却要生生世世 2024-12-11 20:46:00

如果你有一个动态的用户体验并且不知道要去找多少个父母,你可以使用这个递归函数来弄清楚......

- (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....
}

If you have a dynamic UX and do not know how many parents to go to, you can use this recursive function to figure it out...

- (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....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文