2012-04-01 107 views
21

我需要將TextField添加到UIAlertView。我明白,蘋果不鼓勵這種方法。那麼是否有任何圖書館可以將TextField添加到UIAlertView外觀相似的框架中?將TextField添加到UIAlertView

回答

4

嘗試這樣:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" 
               message:@"\n\n" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Save", nil] autorelease]; 
CGRect rect = {12, 60, 260, 25}; 
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease]; 
dirField.backgroundColor = [UIColor whiteColor]; 
[dirField becomeFirstResponder]; 
[alert addSubview:dirField]; 

[alert show]; 
+2

我認爲蘋果阻止你的做法。 – shajem 2012-04-01 07:13:45

3

你可以試試:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
[myTextField setBackgroundColor:[UIColor whiteColor]]; 
[myAlertView addSubview:testTextField]; 
[myAlertView show]; 
[myAlertView release]; 

關注這個link查看詳細。

+0

是的,你的代碼有效。但是蘋果不贊成這種方式,因爲它使用私有API。有沒有其他辦法可以解決這個問題? – shajem 2012-04-01 07:17:28

+0

這裏沒有使用私有API .. – 2012-04-01 08:14:03

+0

@anonymous OP引用文檔中的句子:此類的視圖層次結構是私有的,不能修改。 – 2012-04-01 08:53:37

0

指這個...... http://iosdevelopertips.com/undocumented/alert-with-textfields.html這是私人api,如果您將它用於appstore中的應用程序,它可能會被拒絕,但它對於企業發展很好。

+0

感謝您的回覆,但我希望將我的應用發佈到appstore:S – shajem 2012-04-01 07:16:28

+0

比我不會建議您應該實施此操作。 – 2012-04-01 07:19:14

6

我使用的是BlockAlertsAndActionSheets而不是AppleView組件和AlertSys和ActionSheets,因爲我更喜歡blocks-approach。源代碼中還包含BlockTextPromptAlertView,這可能是您想要的。您可以替換該控件的圖像以獲得Apple風格。

Project on gitgub

Tutorial which gets you started

例子:

- (IBAction)newFolder:(id)sender { 
    id selfDelegate = self; 
    UITextField     *textField; 
    BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder" 
                    message   :@"Please enter the name of the new folder!" 
                    textField  :&textField]; 
    [alert setCancelButtonWithTitle:@"Cancel" block:nil]; 
    [alert addButtonWithTitle:@"Okay" block:^{ 
     [selfDelegate createFolder:textField.text]; 
    }]; 
    [alert show]; 
} 

- (void)createFolder:(NSString*)folderName { 
    // do stuff 
} 
+0

你能告訴我一個使用'BlockTextPromptAlertView'的例子。我的印象是開發人員不允許向UIAlertView添加任何文本字段等。 – shajem 2012-04-01 07:15:49

+0

你的印象是對的。這就是爲什麼BlockAlertsAndActionSheets完全不使用UIAlertView的**,而是完全獨立的實現,沒有使用任何私有API。我目前無法訪問我的源代碼,但可以在幾個小時內使用代碼示例更新我的答案。 – 2012-04-01 08:05:39

+0

更新了示例用法 – 2012-04-01 08:47:13

14

不幸的是,這唯一的官方API是iOS 5的最多,這是一個名爲alertViewStyle屬性,它可以設置爲以下參數:

UIAlertViewStyleDefault 
UIAlertViewStyleSecureTextInput 
UIAlertViewStylePlainTextInput 
UIAlertViewStyleLoginAndPasswordInput 

UIAlertViewStylePlainTextInput是你想要的。

蘋果公司強烈建議不要使用上述視圖層次結構。

+0

我必須爲iOS 4和iOS 5創建此項。所以您知道任何其他我可以使用的圖書館 – shajem 2012-04-01 08:01:51

69

對於iOS5的:

UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
av.alertViewStyle = UIAlertViewStylePlainTextInput; 
[av textFieldAtIndex:0].delegate = self; 
[av show]; 

此外,您將需要實現UITextFieldDelegateUIAlertViewDelegate協議。

0

添加從「Shmidt」,代碼捕捉UIAlertView中輸入的文本回答粘貼以下(謝謝你「韋恩哈特曼」 Getting text from UIAlertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     self.userNumber = [alertView textFieldAtIndex:0].text; 
     if (self.userNumber) { 
      // user enetered value 
      NSLog(@"self.userNumber: %@",self.userNumber); 
     } else { 
      NSLog(@"null"); 
     } 

    } 
} 
4

由於iOS的8 UIAlertView已被棄用,取而代之的UIAlertController,這增加了支持添加使用該方法UITextField S:

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler; 

爲一個例子見this answer

1

首先將UIAlertViewDelegate添加到ViewController中。.h文件一樣

#import <UIKit/UIKit.h> 

@interface UIViewController : UITableViewController<UIAlertViewDelegate> 

@end 

,比添加下面,你要提醒顯示代碼,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
              message:@"Message" 
              delegate:self 
            cancelButtonTitle:@"Done" 
            otherButtonTitles:nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

,它的委託方法,它返回的UITextField

什麼輸入
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
NSLog(@"%@", [alertView textFieldAtIndex:0].text); 
}