iPhone带有文本框的警告视图

6
我有一个带有UITextFieldUIAlertView。我想要在UIAlertViewok按钮中输入邮件ID并提交,但是UIAlertView中的UITextField没有响应。请帮助我。
谢谢。
4个回答

19

iOS 5 开始,上述方法已不再必要。只需将 alertViewStyle 属性设置为适当的样式(UIAlertViewStyleSecureTextInputUIAlertViewStylePlainTextInputUIAlertViewStyleLoginAndPasswordInput)即可。

示例:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Enter your email:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

你可以把响应作为返回值

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField *emailTextField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",emailTextField.text);
}

11

带有 UITextFieldUIAlertView:

用法:

NSString *title         = NSLocalizedString(@"Your Title","");
NSString *placeholder   = NSLocalizedString(@"Placeholder Text","");
NSString *message       = NSLocalizedString(@"A message to the user.","");
NSString *cancel        = NSLocalizedString(@"Cancel","");
NSString *okay          = NSLocalizedString(@"Continue","");

prompt = [[AlertPrompt alloc] initWithTitle:title 
                   placeholder:placeholder 
                       message:message 
                      delegate:self 
             cancelButtonTitle:cancel 
                 okButtonTitle:okay];
[prompt show];
[prompt release];

.h

#import <Foundation/Foundation.h>

@interface AlertPrompt : UIAlertView <UITextFieldDelegate>
{
    UITextField *textField;
    NSString *enteredText;
}
@property(nonatomic,retain) UITextField *textField;
@property (nonatomic, retain, readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle;
@end

.m

#import ".h"

@implementation AlertPrompt

@synthesize textField;
@synthesize enteredText;

#pragma mark -
#pragma mark AlertPrompt Delegates

- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate 
  cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle {

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        self.title      = title;
        self.message      = [NSString stringWithFormat:@"%@\n\n\n",message];
        self.delegate    = delegate;

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 75.0f, 260.0, 30.0)]; 
        [theTextField setBackgroundColor:[UIColor clearColor]]; 
        theTextField.borderStyle = UITextBorderStyleRoundedRect;
        theTextField.textColor = [UIColor blackColor];
        theTextField.font = [UIFont systemFontOfSize:18.0];
        theTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
        theTextField.returnKeyType = UIReturnKeyDone;
        theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        theTextField.placeholder = placeholder;
        theTextField.delegate = self;
        [self insertSubview:theTextField atIndex:0];
        self.textField = theTextField;
        [theTextField release];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -10); 
        [self setTransform:translate];
    }
    return self;
}

- (void)show{
    [textField becomeFirstResponder];
    [super show];
}

- (NSString *)enteredText{
    return textField.text;
}

- (void)dealloc{
    [textField resignFirstResponder];
    [textField release];
    [super dealloc];
}
@end

代表:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if([alertView.title isEqualToString:@"Your Title"])
    {    
        if(buttonIndex == 1)
        {
            /*  get the user iputted text  */
            NSString *inputValue = [(AlertPrompt *)alertView enteredText];
            NSLog(@"User Input: %@",inputValue);
        {
    }
}

这是一个非常详细的答案 +1 - Sudhanshu
@Jordan 不,这个例子没有什么“未记录的”。我实际上在iTunes应用商店中有4个应用程序使用了这段代码,并且获得了批准没有任何问题。 - WrightsCS
@WrightsCS - 我并没有说它不能工作,或者不会被批准。只是它没有被记录下来。这意味着行为可能会在未来发生变化。 - Jordan
@Jordan,不,这里没有潜在的行为变化。这都是标准的东西。在这个例子中,有什么可能会在未来发生改变呢?我的意思是,对于苹果公司,你可能会遇到一些变化,但你需要适应并更新你的代码。 - WrightsCS
这在iOS 4下运行得很好,但在iOS 5 beta下,文本框显示不正确--它没有边框或背景,所以看起来像你是直接在UIAlertView上输入而不是在UITextField内部。有什么建议吗? - Jason
显示剩余3条评论

0

Swift 版本:

var alert = UIAlertView(title: "PostActionTitle", message: "PostActionText", delegate: self, cancelButtonTitle:"PostActionDecline")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("PostActionAccept")
alert.show()

0
UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"Enter your Email-id" message:@"\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    emailTextField=[[UITextField alloc]initWithFrame:CGRectMake(12,45,260,25)];
    [emailTextField becomeFirstResponder];

    [emailTextField setBackgroundColor:[UIColor whiteColor]];
    emailTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
        emailTextField.placeHolder=@"Email";
    [emailAlert addSubview:emailTextField];
    [emailAlert show];
    [emailAlert release];

当点击OK按钮时,请使用UIAlertView委托方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接