UIViewControllerHierarchyInconsistency - 视图一次最多只能有一个视图控制器

时间:2012-09-25 01:42:16

标签: 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:terminate调用抛出异常 [切换到进程9219线程0x2403]

我认为这是导致问题的原因;

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




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

当我点击按钮时,应该将我带到MFMail作曲家,这是应用程序崩溃的时候。

有什么想法吗?

提前致谢

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

    // 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 *)error {     //通知用户与界面相关的错误 开关(结果) {     case MFMailComposeResultCancelled:         打破;     case MFMailComposeResultSaved:         打破;     case MFMailComposeResultSent:         打破;     case 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]; }

1 个答案:

答案 0 :(得分:1)

使邮件组成视图控制器显示的主要事情是使用当前活动的UIViewController来呈现它。您没有分享足够的代码以便我清楚,但如果self是该上下文中的UIViewController,您需要这样做:

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

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