与textfield的iphone alertview

Nip*_*P N 6 iphone cocoa-touch objective-c uialertview

我有UIAlertView一个UITextField在里面.我想键入邮件ID并提交UIAlertViewOK按钮,但UITextFieldUIAlertView没有响应,请帮帮我.

thankz

小智 20

从iOS 5开始,不再需要上述方法.只需将设置alertViewStyle属性合适的样式(UIAlertViewStyleSecureTextInput,UIAlertViewStylePlainTextInput,或UIAlertViewStyleLoginAndPasswordInput).

例:

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

你可以把回复作为

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


Wri*_*sCS 11

UIAlertViewUITextField:

用法:

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];
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

代表:

-(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);
        {
    }
}
Run Code Online (Sandbox Code Playgroud)