presentmodalviewcontroller在iPhone中的Application Delegate中无法正常工作

时间:2010-05-12 12:12:00

标签: iphone objective-c uiviewcontroller

我在Application Delegate中使用了两个UIViewController,并使用presentmodalviewcontroller导航到UIViewController。但问题是presentmodalviewcontroller第一次工作UIViewController,当我想使用presentmodalviewcontroller导航到第二个UIViewController然后它显示第一个UIViewController。 以下是代码: -

-(void)removeTabBar:(NSString *)str
{
    HelpViewController *hvc =[[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:[NSBundle mainBundle]];
    VideoPlaylistViewController *vpvc =[[VideoPlaylistViewController alloc] initWithNibName:@"VideoPlaylistViewController" bundle:[NSBundle mainBundle]];
    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];
    }
}

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

每次运行此功能时,您都会创建一个新的hvcvpvc

第一次,我假设您致电removeTabBar:@"Help",它会生成hvcvpvc,然后会显示正确的。{/ p>

第二次调用removeTabBar:@"VideoPlayList"时,您正在制作新的hvcvpvc。这意味着,当您致电hvc dismissModalViewController:YES];时,您没有删除之前添加的那个,那么您将删除刚刚创建的新内容,而这个内容根本没有显示!

要解决此问题,您需要将两个控制器作为应用委托中的属性委托,并使用applicationDidFinishLaunching方法创建它们。

将这些添加到您的app delegate的.h文件中:

@class MyAppDelegate {
    HelpViewController *hvc;
    VideoPlaylistViewController *vpvc;
}

@property (nonatomic, retain) HelpViewController *hvc;
@property (nonatomic, retain) VideoPlaylistViewController *vpvc;

@end

并在您的app delegate的.m文件中:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    ...
    self.hvc = [[[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil] autorelease];
    self.vpvc = [[[VideoPlaylistViewController alloc] initWithNibName:@"VideoPlaylistViewController" bundle:nil] autorelease];
    ...
}

并删除removeTabBar

中的前两行