在 UIAlertView 中创建了 3 个 UITextField 但没有采取行动
Created 3 UITextField in UIAlertView but not getting action
我创建了一个包含三 (3) 个 UITextField 的 UIAlertView。我如何获得 UIAlertView 的动作。 clickedButtonAtIndex 委托方法正在运行。
但是没有得到我在字段中输入的文本。有什么想法吗? 在此先感谢。
UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
v_alert.tag=10;
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Your Previous Password";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[v_alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Your New Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[v_alert addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[v_alert addSubview:textField3];
alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertV setValue:v_alert forKey:@"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
在委托中:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");
UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0];
UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];
UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2];
NSLog(@"%@--%@--%@",txt1.text,txt2.text,txt3.text);
}
首先设置textField委托
<UITextFieldDelegate>
然后我尝试了下面的代码
NSString *strAddress = @"14 UPPER CIRCULAR ROAD, Singapore 058412";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strAddress delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Username";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[v addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[v addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Address";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
[v addSubview:textField3];
[alert setValue:v forKey:@"accessoryView"];
[alert show];
TextField 委托方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"The delegate method is called");
return YES;
}
另见我的屏幕截图
输出结果为
The delegate method is called
声明您的 3 TextField
UITextField *textField1, *textField2, *textField3;
然后用您的代码替换此代码。
UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
alertview.backgroundColor = [UIColor whiteColor];
alertview.tag=10;
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Your Previous Password";
textField1.secureTextEntry = YES;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[alertview addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Your New Password";
textField2.secureTextEntry = YES;
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[alertview addSubview:textField2];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[alertview addSubview:textField3];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertV setValue:alertview forKey:@"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
现在你的 UIAlertView 按钮方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(@"Button No click");
}
if (buttonIndex == 1) {
NSLog(@"Button Yes Cancel");
}
NSLog(@"%@--%@--%@",textField1.text,textField2.text,textField3.text);
}
输出:
宝石--Moradiya--Himmoradiya@gmail.com
这就是我接受的技巧。但标准方法是最新的 iOS:
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Change Password"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UITextField *alertText1 = [alert.textFields objectAtIndex:0];
UITextField *alertText2 = [alert.textFields objectAtIndex:1];
UITextField *alertText3 = [alert.textFields objectAtIndex:2];
if(alertText1.text.length>0&&alertText1.text.length>0)
{
NSLog(@"Resolving UIAlert Action for tapping OK Button:%@-%@-%@",alertText1.text,alertText2.text,alertText3.text);
oldpPass=alertText1.text;
NewPass=alertText2.text;
[self update_password];
}
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(@"Resolving UIAlertActionController for tapping cancel button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"Enter old password";
textField.accessibilityIdentifier = @"paswordTextField";
textField.accessibilityLabel = @"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"Enter New password";
textField.accessibilityIdentifier = @"paswordTextField";
textField.accessibilityLabel = @"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.accessibilityIdentifier = @"usernameTextField";
textField.placeholder = @"Enter email";
textField.text=self.emailTF.text;//
textField.accessibilityLabel = @"usernameLabel";
}];
[self presentViewController:alert animated:YES completion:nil];
我创建了一个包含三 (3) 个 UITextField 的 UIAlertView。我如何获得 UIAlertView 的动作。 clickedButtonAtIndex 委托方法正在运行。
但是没有得到我在字段中输入的文本。有什么想法吗? 在此先感谢。
UIView *v_alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
v_alert.tag=10;
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Your Previous Password";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[v_alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Your New Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[v_alert addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[v_alert addSubview:textField3];
alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertV setValue:v_alert forKey:@"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
在委托中:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");
UITextField*txt1=[[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:0];
UITextField*txt2= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:1];
UITextField*txt3= [[[self.view viewWithTag:100] viewWithTag:10] viewWithTag:2];
NSLog(@"%@--%@--%@",txt1.text,txt2.text,txt3.text);
}
首先设置textField委托
<UITextFieldDelegate>
然后我尝试了下面的代码
NSString *strAddress = @"14 UPPER CIRCULAR ROAD, Singapore 058412";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strAddress delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Username";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[v addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[v addSubview:textField2];
UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Address";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
[v addSubview:textField3];
[alert setValue:v forKey:@"accessoryView"];
[alert show];
TextField 委托方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"The delegate method is called");
return YES;
}
另见我的屏幕截图
输出结果为
The delegate method is called
声明您的 3 TextField
UITextField *textField1, *textField2, *textField3;
然后用您的代码替换此代码。
UIView *alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
alertview.backgroundColor = [UIColor whiteColor];
alertview.tag=10;
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Your Previous Password";
textField1.secureTextEntry = YES;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
textField1.tag=1;
[alertview addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Your New Password";
textField2.secureTextEntry = YES;
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
textField2.tag=2;
[alertview addSubview:textField2];
textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Email";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
textField3.tag=3;
[alertview addSubview:textField3];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Change Password" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alertV setValue:alertview forKey:@"accessoryView"];
alertV.tag==100;
alertV.delegate=self;
[alertV show];
现在你的 UIAlertView 按钮方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(@"Button No click");
}
if (buttonIndex == 1) {
NSLog(@"Button Yes Cancel");
}
NSLog(@"%@--%@--%@",textField1.text,textField2.text,textField3.text);
}
输出: 宝石--Moradiya--Himmoradiya@gmail.com
这就是我接受的技巧。但标准方法是最新的 iOS:
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Change Password"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UITextField *alertText1 = [alert.textFields objectAtIndex:0];
UITextField *alertText2 = [alert.textFields objectAtIndex:1];
UITextField *alertText3 = [alert.textFields objectAtIndex:2];
if(alertText1.text.length>0&&alertText1.text.length>0)
{
NSLog(@"Resolving UIAlert Action for tapping OK Button:%@-%@-%@",alertText1.text,alertText2.text,alertText3.text);
oldpPass=alertText1.text;
NewPass=alertText2.text;
[self update_password];
}
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(@"Resolving UIAlertActionController for tapping cancel button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"Enter old password";
textField.accessibilityIdentifier = @"paswordTextField";
textField.accessibilityLabel = @"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"Enter New password";
textField.accessibilityIdentifier = @"paswordTextField";
textField.accessibilityLabel = @"passwordLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.accessibilityIdentifier = @"usernameTextField";
textField.placeholder = @"Enter email";
textField.text=self.emailTF.text;//
textField.accessibilityLabel = @"usernameLabel";
}];
[self presentViewController:alert animated:YES completion:nil];