当Modal视图控制器关闭时,应用程序崩溃

时间:2013-08-12 13:12:11

标签: ios objective-c uiviewcontroller

我想从应用的第二个屏幕显示modalViewControlle r,当我关闭控制器时,它应该导航到第一个屏幕。以下代码适用于iPhone 4,5和iPod Touch,但不适用于iPAD。目标是当我解除modalViewController它不应该回到第二个屏幕时,它应该显示第一个屏幕。

        ShareEmail *shareEmail = [[ShareEmail alloc] initWithNibName:[NSString stringWithFormat:@"%@",xibShareEmail] bundle:nil];

        shareEmail.fromWer = @"ownPer";
        [self presentModalViewController:shareEmail animated:NO];
        [shareEmail release];

        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

在分享电子邮件类

[self dismissModalViewControllerAnimated:YES];

2 个答案:

答案 0 :(得分:1)

您需要保留您的shareEmail视图控制器 - 创建合成的保留属性

YourClass.h:

@property (nonatomic, retain) ShareEmail *shareEmailViewController;

YourClass.m:

@synthesize shareEmailViewController;

然后通过以下方式显示模态视图控制器:

ShareEmail *shareEmail = [[ShareEmail alloc] initWithNibName:[NSString stringWithFormat:@"%@",xibShareEmail] bundle:nil];
self.shareEmailViewController = shareEmail;
[self presentModalViewController:shareEmail animated:NO];
[shareEmail release];

保留属性将使视图控制器在使用时不被释放。然后,当你稍后解雇它时,你可以这样做:

[self.shareEmailViewController dismissModalViewControllerAnimated:YES];
self.shareEmailViewController = nil;

将释放保留属性并在完成后释放内存。

答案 1 :(得分:0)

有趣的事情正在发生。您正在使用self呈现视图控制器,然后您正在navigationController执行其向后导航。此时,我之前谈论的self消失了。

popViewController方法中删除presentViewController方法。

此外,您还需要使用^completion处理程序方法。这就是你应该放置导航控制器pop代码的地方。

-(void)present {
    ShareEmail *email = [[ShareEmail...
    //You'll need to get a weak reference to `self` in `email`
    email.modalDelegate = self; //you need to make a property in ShareEmail
    [self presentViewController:email animated:YES completion:nil];
    [email release];
}

-(void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

//in ShareEmail.m
[modalDelegate dismiss];