开发者

dismissing modalViewController from UITabBarController issue

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开发者_JS百科 called now. The issue now is that when I call [self.tabBarController dismissModalViewControllerAnimated:YES] it doesn't dismiss the modal view controller.


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]"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜