presentmodalviewcontroller not working properly in Application Delegate in iPhone
I'm using two UIViewController in Application Delegate and navigating to UIViewController using presentmodalviewcontroller. But Problem is that presentmodalviewcontroller works for first time UIViewController and when i want to navigate to second UIViewController using presentmodalviewcontroller then its showing first UIViewController. The following is the code:-
-(void)removeTabBar:(NSString *)str
{
HelpViewController *hvc =[[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:[NSBundle mainBundle]];
VideoPlaylistViewController *vpvc =[[VideoPlaylistViewController alloc] initWithNibName:@"VideoPlaylistViewController" bundle:[NSBundle mainBundle]];
开发者_C百科 if ([str isEqualToString:@"Help"])
{
[tabBarController.view removeFromSuperview];
[vpvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:hvc animated:YES];
[hvc release];
}
if ([str isEqualToString:@"VideoPlaylist"])
{
[hvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:vpvc animated:YES];
[vpvc release];
}
}
Can Somebody help me in solving the problem?
You're making a new hvc
and vpvc
each time you run this function.
The first time through, I assume you call removeTabBar:@"Help"
, it makes a hvc
and vpvc
and then shows the correct one.
The second time you call it removeTabBar:@"VideoPlayList"
, you are making a new hvc
and vpvc
. This means that when you call hvc dismissModalViewController:YES];
you're not removing the one you added before, you're removing the new one that you just made which isn't being displayed at all!
To solve this you need to make your two controllers as properties in your app delegate and create them in the applicationDidFinishLaunching
method.
Add these into your app delegate's .h file:
@class MyAppDelegate {
HelpViewController *hvc;
VideoPlaylistViewController *vpvc;
}
@property (nonatomic, retain) HelpViewController *hvc;
@property (nonatomic, retain) VideoPlaylistViewController *vpvc;
@end
and in your app delegate's .m file :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
self.hvc = [[[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil] autorelease];
self.vpvc = [[[VideoPlaylistViewController alloc] initWithNibName:@"VideoPlaylistViewController" bundle:nil] autorelease];
...
}
and remove the first two lines in removeTabBar
精彩评论