iphone - adding the view of MFMailComposeViewContr

2019-03-04 13:09发布

I've spent the past two days just trying to enable the sending of email from within my app. Hoping one of the smart folks on here can help me out. presentModalViewController doesn't work for me (just crashes the app with no explanation as to why), so I'm forced to add the view of MFMailComposeViewController. Here's my attempt:

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"test subject"];
    [controller setMessageBody:@"this is the message body" isHTML:NO];

    //  [self presentModalViewController:controller animated:YES]; //this crashes the app
//so I try this instead:        
    controller.view.frame = CGRectMake(0,0,480,320);
    [self.view addSubview:controller.view];
    [controller release];

What gets added to the screen is the subject bar only, with cancel and send buttons. None of the text fields (To:, Cc:, Subject, body) are displayed. Why aren't they a part of MFMailComposeViewController's view, and how can I display them?

5条回答
贼婆χ
2楼-- · 2019-03-04 13:27

You should instead try:

[[self navigationController] presentModalViewController...];

Since that's the proper way to present it. Trying to add its view manually is unfortunately utterly incorrect and will never work.

查看更多
姐就是有狂的资本
3楼-- · 2019-03-04 13:30

Well I have determined that one must create a dummy view controller otherwise the darn thing won't slide in.

I create a class called Sys_Mail that is a @interface Sys_Mail : UIViewController <MFMailComposeViewControllerDelegate>

and then i create basically a root view view controller. I wrestled with portrait/landscape for hours but determined that if you attach the view controller to the top level view (which contains my landscape transform) then it slides in as a landscape window. There is just one visual glitch, the parent window gets moved around for a few seconds while the new window slides in, this is a side effect of the landscape transform doing odd things to the parent....

in order to get landscape orientation on the sliding window you must declare a method in your Sys_Mail class that handles the autorotate message:

//=======================
//    shouldAutorotateToInterfaceOrientation
//=======================
//  see if this ever gets called for the view controller
-(BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation 
{ 
  if (TRACE) printf ("shouldAutorotateToInterfaceOrientation\n");
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);  // or whatever orientation is needed
}

the variable gMasterView refers to my top level view (that has the landscape transform and is attached to the window). Subviews don't seem to work, view controllers are awful THEY ARE MORE DESIGN PATTERN CRAP. I want total control of my views not some microsoft MFC type crud!

Sys_Mail* g_root_vc;

if (g_root_vc == nil) {
  //  create an empty view controller so we have something to work with
  g_root_vc = [[Sys_Mail alloc] init];
  g_root_vc.view = (UIView*) gMasterView; 
}

so this

查看更多
一夜七次
4楼-- · 2019-03-04 13:34

I 've solved this problem:

try NOT this:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];

but THIS:

MFMailComposeViewController *mailComposeViewController = [MFMailComposeViewController new];
查看更多
Juvenile、少年°
5楼-- · 2019-03-04 13:35

Honestly, you should be using presentModalViewController. Rather than force your way around the SDK, consider debugging the crash. Turn on the debugger and see if there are any exceptions logged in the console. Check for crash logs, etc...

Also, make sure that self is a proper delegate and a UIViewController subclass.

查看更多
淡お忘
6楼-- · 2019-03-04 13:41

I have the same crash and finally I can fix it by sending presentModalViewController message to [self navigationController].

Here is my code:

// Create the Mail composer view controller
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];

// Set the view controller delegate
controller.mailComposeDelegate = self;

// Set recipients, if you want
[controller setToRecipients:recipients];

// Set subject, if you want
[controller setSubject:@"The subject"];

// Set message body, if you want
[controller setMessageBody:@"The message body" isHTML:YES]; // isHTML -> YES/NO depending the message body

// Present the view controller 
[[self navigationController] presentModalViewController:controller animated:YES];

// Memory management
[controller release];

I hope this can help!

查看更多
登录 后发表回答