gpt4 book ai didi

ios - 如何在 Objective-C 中获取 UIAlertView subview UITextField 值

转载 作者:行者123 更新时间:2023-12-01 18:53:41 27 4
gpt4 key购买 nike

伙计们,我刚刚创建了一个 alertview ,在 subview 中有 3 个字段,如下所示:

//----adding an alertview----
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Vehicle Information" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
UIView *view = [[UIView alloc] initWithFrame:frame];

UITextField* text1 = [[UITextField alloc] initWithFrame: CGRectMake(10, 10,self.view.frame.size.width-125, 30)];
text1.backgroundColor=[UIColor whiteColor];
UITextField* text2 = [[UITextField alloc] initWithFrame: CGRectMake(10, 45, self.view.frame.size.width-125, 30)];
text2.backgroundColor=[UIColor whiteColor];
UITextField* text3 = [[UITextField alloc] initWithFrame: CGRectMake(10, 80, self.view.frame.size.width-125, 30)];
text3.backgroundColor=[UIColor whiteColor];
text1.layer.borderColor=[UIColor lightGrayColor].CGColor;
text1.layer.borderWidth=1;
text2.layer.borderColor=[UIColor lightGrayColor].CGColor;
text2.layer.borderWidth=1;
text3.layer.borderColor=[UIColor lightGrayColor].CGColor;
text3.layer.borderWidth=1;
text1.placeholder=@"Year";
text2.placeholder=@"Make";
text3.placeholder=@"Model";
[view addSubview:text1];
[view addSubview:text2];
[view addSubview:text3];
[alertView setValue:view forKey:@"accessoryView"];
view.backgroundColor = [UIColor whiteColor];
alertView.tag = 1;
[alertView show];

enter image description here

现在我想获取文本字段值,单击保存按钮确实调用了以下类,但我没有得到值。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"AlertView is Active");
if (alertView.tag == 1 && buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"alert view working");
NSString *Year = [[alertView textFieldAtIndex:0] text];
NSLog(@"%@",Year);
}

}

我得到一个空值,如下所示:

enter image description here

最佳答案

[alertView textFieldAtIndex:] 仅在您设置 alertView.alertViewStyle 时有效。

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};

关于ios - 如何在 Objective-C 中获取 UIAlertView subview UITextField 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com