MFMessageComposeViewController关闭键盘

时间:2012-02-10 09:55:56

标签: iphone ios ios4 ios5

我的MFMessageComposeViewController遇到了麻烦。我想在应用程序中使用SMS。 一切正常,发送短信,到目前为止一切顺利。但当我点击取消按钮(或发送按钮)时,我的视图顶部消失,但键盘没有。这可能是因为我不使用modale视图,而只使用addSubview。

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{

    if (result == MessageComposeResultCancelled)
    {
        NSLog(@"Message annulé");
        [controller resignFirstResponder];
        [controller.view removeFromSuperview];  
        [controller release]; 
     }
    else if (result == MessageComposeResultSent)
    {
        NSLog(@"Message envoyé");
        ...
    }
    else 
    {
        NSLog(@"Message non envoyé");
        ...
    }
}

-(void)sendSMS:(NSString *)bodyOfMessage :(Phone *)recipient
{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    if([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;

        NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
        [toRecipients addObject:recipients.phoneNumber];
        [picker setRecipients:(NSArray *)toRecipients];
        [toRecipients release];

        NSString *bodyString = nil;
        bodyString = bodyOfMessage;
        [picker setBody:bodyString];

        [self addSubView:picker.view];
        [picker release];
     }  
}

有什么想法吗?我是否只使用modalView?

抱歉拼写错误...

谢谢。托米

3 个答案:

答案 0 :(得分:1)

是的,你必须使用modalviewcontroller。

[self presentModalViewController:picker];

此外,您正在创建MFMessageComposeViewController的两个实例,首先检查它是否可以发送文本,然后另一个实际显示它。我建议只创建一个,它对内存更好:)也是第一个泄漏,因为你没有发布它。祝你好运!

答案 1 :(得分:0)

    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        NSString *bodyString = nil;

        NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
        [toRecipients addObject:@"phone here"];
        [picker setRecipients:(NSArray *)toRecipients];
        [toRecipients release];

        bodyString = [NSString stringWithFormat: @"Message body"];
        [picker setBody:bodyString];

        [self presentModalViewController:picker animated:YES];
        [picker release];
}

答案 2 :(得分:0)

在使用MFMessageComposeViewController呈现模态视图控制器之前,请尝试关闭现有键盘:

[self.view endEditing:YES]; //close keyboard if it opened
[self presentModalViewController:messageController animated:YES];