从 UITabBarController 问题中消除 modalViewController

发布于 2024-12-06 02:59:09 字数 1407 浏览 0 评论 0原文

因此,在我的应用程序委托中,我尝试通过执行以下操作从 UITabBarController 呈现 modalViewController:

self.tabBarController = [[UITabBarController alloc] init];
LoginViewController* loginViewController = [[LoginViewController alloc] init];
        loginViewController.delegate = self;
        [self.tabBarController presentModalViewController:loginViewController animated:NO];
        [loginViewController release];

并且应用程序委托中定义的委托是:

- (void)userDidLogin:(LoginViewController *) loginViewController
{
    NSLog(@"DELEGATE CALLED, DISMISSING");
    [self.tabBarController dismissModalViewControllerAnimated:NO];
}

这是我的 LoginViewController:

protocol LoginViewControllerDelegate;

@interface LoginViewController : UIViewController <MBProgressHUDDelegate>
{


    id<LoginViewControllerDelegate> delegate;
}

@property (assign) id<LoginViewControllerDelegate> delegate;

@end


@protocol LoginViewControllerDelegate

- (void)userDidLogin:(LoginViewController *) loginViewController;

@end

问题是这个 (userDidLogin:(LoginViewController *) loginViewController) 从未被调用...这是为什么? 我在 LoginViewController 实现中调用了以下内容,这称为

[self.delegate userDidLogin:self];

更新:

我现在调用了委托。现在的问题是,当我调用 [self.tabBarController DismissModalViewControllerAnimated:YES] 时,它不会关闭模式视图控制器。

so in my app delegate I am trying to present a modalViewController from a UITabBarController, by doing the following:

self.tabBarController = [[UITabBarController alloc] init];
LoginViewController* loginViewController = [[LoginViewController alloc] init];
        loginViewController.delegate = self;
        [self.tabBarController presentModalViewController:loginViewController animated:NO];
        [loginViewController release];

and the delegate defined in the app delegate is:

- (void)userDidLogin:(LoginViewController *) loginViewController
{
    NSLog(@"DELEGATE CALLED, DISMISSING");
    [self.tabBarController dismissModalViewControllerAnimated:NO];
}

Here's my LoginViewController:

protocol LoginViewControllerDelegate;

@interface LoginViewController : UIViewController <MBProgressHUDDelegate>
{


    id<LoginViewControllerDelegate> delegate;
}

@property (assign) id<LoginViewControllerDelegate> delegate;

@end


@protocol LoginViewControllerDelegate

- (void)userDidLogin:(LoginViewController *) loginViewController;

@end

The issue is that this (userDidLogin:(LoginViewController *) loginViewController) is never called... why is this?
I have called the following in my LoginViewController implementation and this is called

[self.delegate userDidLogin:self];

UPDATE:

I got the delegate called now. The issue now is that when I call [self.tabBarController dismissModalViewControllerAnimated:YES] it doesn't dismiss the modal view controller.

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

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

发布评论

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

评论(1

暮倦 2024-12-13 02:59:09

您没有从 LoginViewController 发布任何代码,但在该类的代码中,当您准备好关闭它时(也许当用户单击“登录”按钮并且登录成功时),您需要添加以下行。

if (delegate && [delegate respondsToSelector:@selector(userDidLogin:)])
     [delegate performSelector:@selector(userDidLogin:) withObject:self];

更新:

我想我明白这里的问题是什么。根据Apple的文档,当您调用presentModalViewController:animated:时,该方法会设置UIViewController(在本例中为UITabBar)的“modalViewController”属性的值。然而,该属性仅维护对 modalViewController 的弱引用。这很重要,因为您初始化 LoginViewController,将其传递给presentModalViewController:animated:,然后释放它。由于presentModalViewController:animated: 没有保留对LoginViewController 的强引用,因此UITTabBar 稍后无法将其关闭。事实上,我很惊讶你所做的并没有导致 EXC_BAD_ACCESS 崩溃。我建议您删除“[loginViewController release]”语句,并在调用“[self.tabBarController解雇ModalViewControllerAnimated:NO]”后释放它

You didn't post any code from LoginViewController, but within that class's code you need to add the following lines when you are ready to dismiss it (perhaps when the user clicks the "Login" button and the login is successful).

if (delegate && [delegate respondsToSelector:@selector(userDidLogin:)])
     [delegate performSelector:@selector(userDidLogin:) withObject:self];

UPDATE:

I think I understand what the issue is here. According to Apple's documentation, when you call presentModalViewController:animated: the method sets the value of the "modalViewController" property of UIViewController (in this case your UITabBar). However that property only maintains a weak reference to the modalViewController. That's important because you initialize the LoginViewController, pass it in to presentModalViewController:animated: and then you release it. Since presentModalViewController:animated: is not retaining a strong reference to the LoginViewController, the UITTabBar is unable to dismiss it later on. In fact I'm surprised what you have done is not resulting in an EXC_BAD_ACCESS crash. I suggest you remove the "[loginViewController release]" statement and instead release it after you call "[self.tabBarController dismissModalViewControllerAnimated:NO]"

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