2012-04-01 106 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); 
}