ios - UIViewControllerHierarchyInconsistency - 一个 View 一次最多只能有一个 View Controller

标签 ios xcode ios6

我在更新我的一个旧应用程序时遇到问题,这只是因为我更新到 Xcode 4.5 并尝试在 iOS6 上出现以下错误;

*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <MainView: 0x1e818620; frame = (0 0; 320 476); autoresize = RM+BM; layer = <CALayer: 0x1e818750>> is associated with <MainViewController: 0x1e811850>. Clear this association before associating this view with <UIViewController: 0x21149860>.'

* 首先抛出调用栈: (0x377d82a3 0x35ae897f 0x377d81c5 0x38b0dd7b 0x38b0dc6b 0x1dc25 0x38b940ad 0x38b9405f 0x38b9403d 0x38b938f3 0x38b93de9 0x38abc5f9 0x38aa9809 0x38aa9123 0x37c5c5a3 0x37c5c1d3 0x377ad173 0x377ad117 0x377abf99 0x3771eebd 0x3771ed49 0x37c5b2eb 0x38afd301 0x1bf03 0x1bea8) libc++abi.dylib:终止调用抛出异常 [切换到进程9219线程0x2403]

我认为是这里导致了问题;

 mailComposer = [[UIViewController alloc] init];
[mailComposer setView:self];
[mailComposer setModalTransitionStyle: UIModalTransitionStyleCoverVertical];




MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];   picker.mailComposeDelegate = self;

当我点击应该将我带到 MFMail 编辑器的按钮时,应用程序就会崩溃。

有什么想法吗?

提前致谢

编辑 - 如果我删除前 3 行应用程序不会崩溃,但是不会加载邮件 View ...这是完整的代码;

    // The actual mail window call



mailComposer = [[UIViewController alloc] init];
[mailComposer setView:self];
[mailComposer setModalTransitionStyle: UIModalTransitionStyleCoverVertical];




MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];   picker.mailComposeDelegate = self;

[picker setSubject:@"Hello How are you"];

    // Fill out the email body text
NSString *pageLink = @"http://www.apple.com"; 
NSString *iTunesLink = @"http://itunes.apple.com/gb/artist/randomer";
NSString *content = @"blah blah"; 


NSString *emailFileName = [NSString stringWithFormat:@"email_.html"];


NSString *emailFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: emailFileName];
NSString *body = [NSString stringWithContentsOfFile:emailFilePath encoding:NSUTF8StringEncoding error:nil];


body = [body stringByReplacingOccurrencesOfString:@"//TEXT_PLACEHOLDER//" withString:@"replace here"];
body = [body stringByReplacingOccurrencesOfString:@"//FRIENDNAME_PLACEHOLDER//" withString:pageLink];
body = [body stringByReplacingOccurrencesOfString:@"//ITUNES_PLACEHOLDER//" withString:  iTunesLink];
body = [body stringByReplacingOccurrencesOfString:@"//PASSKEY_PLACEHOLDER//" withString:  yourPassword];
body = [body stringByReplacingOccurrencesOfString:@"//PHONETIC_PLACEHOLDER//" withString:  yourPhoneticPassword];




[picker setMessageBody:body isHTML:YES];    
picker.navigationBar.barStyle = UIBarStyleBlack;    
[mailComposer presentModalViewController:picker animated:YES];
[picker release];

//HANDLE THE MAIL EVENTS
////////////////////////
  • (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)错误 { //通知用户与接口(interface)相关的错误 切换(结果) { 案例 MFMailComposeResultCancelled: 休息; 案例 MFMailComposeResultSaved: 休息; 案例 MFMailComposeResultSent: 休息; 案例 MFMailComposeResultFailed: 休息;

    default:
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-("
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    
        break;
    

    } [mailComposer dismissModalViewControllerAnimated:YES]; //[mailComposer.view.superview removeFromSuperview];

最佳答案

让邮件撰写 View Controller 显示的主要事情是使用当前事件的 UIViewController 显示它。你没有分享足够的代码让我清楚这一点,但如果 self 在那个上下文中是一个 UIViewController,你需要做:

[self presentViewController:picker animated: YES completion:nil];

如果 self 不是 UIViewController,请将上面代码中的 self 替换为引用当前 UIViewController 的变量。

关于ios - UIViewControllerHierarchyInconsistency - 一个 View 一次最多只能有一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12575176/

相关文章:

ios - 应用程序在 iOS 中将帐户 A 移动到 B 后可能丢失钥匙串(keychain)访问问题

ios - 断点卡在主函数处

iphone - 使用iOS6中的新 map ,是否可以更改图层的颜色(公园、水、建筑物)

ios - 如何在 ARKit 中的 SCNPlane 上渲染具有透明背景的 UIView?

ios - 为什么 Storyboard 中的静态原型(prototype) UITableViewCells 显示为空白?

ios - UIButton - 给按钮一个 Action ?

objective-c - 如何在编译时忽略一些静态库

ios - 更新 Xcode 项目时出错。从 Xamarin.Studio 打开 XIB 时参数无效

ios - 返回没有导航 Controller 的导航栏(如 android 中的 Activity.this.finish())?

iphone - VIewController/Views 上的数据保留