2009-11-06 49 views
6

我使用Apple的MailComposer示例應用程序從我的應用程序(OS 3.0功能)中發送電子郵件。是否可以使用MFMailComposeViewController將To,Subject或Body字段設置爲第一響應者?在MFMailComposeViewController中設置第一響應者?

換句話說,行爲將是:用戶按下提供郵件視圖的按鈕(presentModalViewController)。當顯示郵件視圖時,光標放置在其中一個字段中並打開鍵盤。

我注意到MFMailComposeViewController文件說:

「重要提示:郵件撰寫界面本身不是定製的,不得通過您的應用程序進行修改此外,呈現的界面後,你的應用程序不允許對電子郵件內容做進一步修改,用戶仍然可以使用界面編輯內容,但忽略編程變更,因此必須在顯示界面之前設置內容字段的值。

但是,我不在乎定製界面。我只想設置那個firstResponder。有任何想法嗎?

回答

1

您可以嘗試在控制器本身上調用becomeFirstResponder。如果這不起作用,您可以嘗試在調試器中獲取郵件撰寫視圖的子視圖列表,直到找到熟悉的文本字段或文本視圖,然後您可以專門用代碼設置響應者狀態,這可能看起來像這(我不知道這是否會工作,但它是一個例子):

[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder] 
8

你可以使這些領域成爲第一響應者。

,如果你將以下方法添加到您的類...

//Returns true if the ToAddress field was found any of the sub views and made first responder 
//passing in @"MFComposeSubjectView"  as the value for field makes the subject become first responder 
//passing in @"MFComposeTextContentView" as the value for field makes the body become first responder 
//passing in @"RecipientTextField"  as the value for field makes the to address field become first responder 
- (BOOL) setMFMailFieldAsFirstResponder:(UIView*)view mfMailField:(NSString*)field{ 
    for (UIView *subview in view.subviews) { 

     NSString *className = [NSString stringWithFormat:@"%@", [subview class]]; 
     if ([className isEqualToString:field]) 
     { 
      //Found the sub view we need to set as first responder 
      [subview becomeFirstResponder]; 
      return YES; 
     } 

     if ([subview.subviews count] > 0) { 
      if ([self setMFMailFieldAsFirstResponder:subview mfMailField:field]){ 
       //Field was found and made first responder in a subview 
       return YES; 
      } 
     } 
    } 

    //field not found in this view. 
    return NO; 
} 

然後,你目前的MFMailComposeViewController後,通過MFMailComposeViewController的觀點到功能與你想成爲第一個響應者現場一起。

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

/*Set up the mail composer*/ 

[self presentModalViewController:mailComposer animated:YES]; 
[self setMFMailFieldAsFirstResponder:mailComposer.view mfMailField:@"RecipientTextField"]; 
[mailComposer release]; 
+0

非常好 - 花了幾個小時試圖找到一些東西來做到這一點。只需要將** RecipientTextField **更改爲** MFRecipientTextField **即可工作。這可以用於App Store提交,或者被認爲是未公開的API? – ayreguitar 2011-05-28 13:22:51

+1

我不確定這是否適用於App Store提交,我只用於內部應用程序。 – holz 2011-05-30 22:38:32

+0

蘋果認可我最近使用過的應用程序,所以看起來使用起來很不錯(至少現在!)再次感謝。 – ayreguitar 2011-06-15 14:27:28

0

我喜歡簡化代碼並使其易於理解。 只需在後面輸入以下代碼:
[self presentModalViewController:mailComposer animated:YES];

for (UIView *subview in mailComposer.view.subviews) { 
    NSString *className = [NSString stringWithFormat:@"%@", [subview class]]; 
    //NSLog(@"%@", className); // list the views - Use this to find another view 
    //The view I want to set as first responder: "_MFMailRecipientTextField" 
    if ([className isEqualToString:@"_MFMailRecipientTextField"]){ 
    [subview becomeFirstResponder]; 
    break; // Stop search. 
    } 
} 
+0

這不起作用,因爲遞歸是向下瀏覽視圖層次結構所必需的。 – Gujamin 2012-11-01 04:11:15

4

在iOS 6中,不能再在任何文本字段AFAICT上設置第一響應者。瀏覽視圖層次結構最終會顯示一個UIRemoteView,這裏的子視圖被混淆了。

相關問題