0

是否可以在 didFinishWithResult 中显示模态视图控制器?

我有一个 iPad 应用程序,它有 3 个视图 Home、Start 和 Login。可以使用主页视图中的开始和登录按钮启动开始和登录模式视图。

如果单击登录按钮,在登录视图中成功登录操作 (didFinishWithResult) 后,我可以关闭登录视图,但我无法启动开始视图。但控件仍保留在主视图上。

对于上述情况,我没有收到任何错误。

应用代理.m:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

HomeViewController  * homeview=[[HomeViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeview];

self.rootViewController = navigationController;
[navigationController setNavigationBarHidden:YES];
self.rootViewController.view.frame = [[UIScreen mainScreen] applicationFrame];
[self.window addSubview:self.rootViewController.view];
[window makeKeyAndVisible];

下面是在主视图中展示登录模式视图的方法

呈现登录模式视图

LoginViewController * vc1 = [LoginViewController loginViewControllerWithNavBar:YES]; 
vc1.boxLoginDelegate = self;
[self presentModalViewController:vc1 animated:YES];

注意:LoginVeiwController 是由 HomeViewController 呈现的模态视图控制器。通过解除 LoginVeiwController 来启动/呈现另一个模态视图控制器(启动控制器)是否正确,如下所示。因为每当 LoginVeiwController 关闭时,控件都会保留在 HomeVeiwContrller 上,而不是启动/呈现 StartViewController:

主页视图

下面是主视图中的方法,它将在成功登录时关闭登录视图并尝试启动开始视图。

- (void)loginViewController:(LoginViewController *)loginViewController didFinishWithResult:(LoginResult)result {
    [self dismissModalViewControllerAnimated:YES];

    startview = [[[startViewController alloc] init] autorelease]; 
    [self.navigationController setNavigationBarHidden:YES];
    [self presentModalViewController:startview animated:YES];

}

如果我在单击按钮时直接显示 StartView,它会很好地启动,但不会在 didFinishWithResult 上启动

4

5 回答 5

0

当你的 currentViewController DissMiss 使用这一行然后尝试下面的代码......

[self performSelector:@selector(PresentView) withObject:nil afterDelay:0.2];

-(void)PresentView
{
    [self.navigationController presentModalViewController:startview animated:YES];
}

希望,这对你有帮助.... :)

于 2012-06-18T18:07:24.247 回答
0

I guess it's about the animation. While the previous controller is dismissing,

 [self presentModalViewController:startview animated:YES];

is called.

You can verify my guess by setting the animation to NO. Or directly use my suggestion to see if it works.

If I am correct, you can set them back to YES. Besides, instead of directly calling the

[self presentModalViewController:startview animated:YES];

You can try:

[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(showController) userInfo:nil repeats:No];

 - (void)showController {
    [self presentModalViewController:startview animated:YES];
 }

edit:

I finally found out how I solve this similar problem before:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{    
    [UIView animateWithDuration:0.5 
                     animations:^{
                            [self dismissModalViewControllerAnimated:YES];
                     } 
                 completion:^(BOOL finished) {
                     NSURL *path = [info objectForKey:@"UIImagePickerControllerMediaURL"];
                     VideoConfirmViewController *videoConfirmCon = [[VideoConfirmViewController alloc]initWithFileURL:path];
                     [self presentModalViewController:videoConfirmCon animated:YES];
                 }
];
}
于 2012-06-18T10:57:10.460 回答
0

Try like this. I think it will be helpful to you.

 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    HomeViewController  * homeview=[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeview];
    [self.window addSubview:navigationController.view];
    [window makeKeyAndVisible];
return YES:
于 2012-06-18T10:57:26.150 回答
0

如果您不需要与非常旧的 iOS 版本兼容,最好使用

 -(void)presentViewControllerAnimated:completion:
 -(void)dismissViewControllerAnimated:completion:

代替

 -(void)presentModalViewController:animated:
 -(void)dismissModalViewController:animated:

在这种情况下

- (void)loginViewController:(LoginViewController *)loginViewController didFinishWithResult:(LoginResult)result 
{
    [self dismissViewControllerAnimated:YES completion: ^{
        startview = [[[startViewController alloc] init] autorelease]; 
        [self.navigationController setNavigationBarHidden:YES];
        [self.presentingViewController presentViewControllerAnimated:YES completion:NULL];

    }];
}
于 2014-01-12T23:22:22.703 回答
0

你需要在你的代码中重构这一行, [self dismissModalViewControllerAnimated:YES];原因是当调用dismiss时,你失去self了控制器的上下文,因此该行 [self presentModalViewController:startview animated:YES];没有进一步显示任何控制器。快速测试只需评论驳回线并亲自查看。

于 2012-06-18T14:56:42.887 回答