2012-02-10 193 views
0

我有我的MFMessageComposeViewController麻煩。我想使用SMS in-app。 一切工作正常發送短信,迄今爲止很好。但是,當我點擊取消按鈕(或發送按鈕)我的觀點頂部消失,但鍵盤沒有。這可能是因爲我不使用modale視圖,而只是一個addSubview。MFMessageComposeViewController關閉鍵盤

-(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?

遺憾的拼寫錯誤...

謝謝。 Tommy

回答

1

是的,你必須使用modalviewcontroller。

[self presentModalViewController:picker]; 

此外,要創建的MFMessageComposeViewController的兩個實例,首先檢查它是否可以發送文本,然後另一個實際表現出來。我建議創建一個,這對內存更好:)也是第一個泄漏,因爲你沒有釋放它。祝你好運!

+0

好的謝謝;) – 2012-02-10 10:52:40

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]; 
} 
0

嘗試關閉現有的鍵盤,MFMessageComposeViewController呈現模式視圖控制器之前:

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