[英]iphone alertview with textfield
I have an UIAlertView
with a UITextField
in it. 我有一个带有
UITextField
的UIAlertView
。 I want to type the mail id and submit in UIAlertView
's ok button, but UITextField
in the UIAlertView
have no response, Please help me. 我想输入邮件ID并在
UIAlertView
的ok按钮中提交,但是UIAlertView
UITextField
没有响应,请帮帮我。
thankz thankz
From iOS 5 the approach above is no longer necessary. 从iOS 5开始,不再需要上述方法。 Just set the
alertViewStyle
property to the appropriate style ( UIAlertViewStyleSecureTextInput
, UIAlertViewStylePlainTextInput
, or UIAlertViewStyleLoginAndPasswordInput
). 只需将
alertViewStyle
属性设置为适当的样式( UIAlertViewStyleSecureTextInput
, UIAlertViewStylePlainTextInput
或UIAlertViewStyleLoginAndPasswordInput
)。
Example: 例:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Enter your email:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
and you can have the response back as 你可以把回复作为
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *emailTextField = [alertView textFieldAtIndex:0];
NSLog(@"%@",emailTextField.text);
}
UIAlertView
with UITextField
: 使用
UITextField
UIAlertView
:
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];
#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
#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);
{
}
}
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];
Use UIAlertView Delegates Methods when OK button is clicked. 单击“确定”按钮时,请使用UIAlertView Delegates方法。
Swift version: Swift版本:
var alert = UIAlertView(title: "PostActionTitle", message: "PostActionText", delegate: self, cancelButtonTitle:"PostActionDecline")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("PostActionAccept")
alert.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.