【问题标题】:How to get an UIAlertView subview UITextField value in Objective-C如何在 Objective-C 中获取 UIAlertView 子视图 UITextField 值
【发布时间】:2015-03-27 08:07:52
【问题描述】:

伙计们,我刚刚创建了一个alertview,在子视图中有 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];

现在我想获取文本字段的值,单击保存按钮确实调用了以下类,但我没有得到值。

- (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);
}

}

我得到一个空值如下:

【问题讨论】:

    标签: ios objective-c uialertview


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我没有得到你的答案,你有其他方法可以访问 text1 text2 text3 吗?
    【解决方案2】:

    您正在将文本字段添加到 UIView 并将其设置为附件视图。要使用 textFieldAtIndex 方法,您必须使用 Apple Document 中提到的不同 UIAlertViewStyle。该文档还明确指出“此类的视图层次结构是私有的,不得修改”。并且 UIAlerView 在 iOS 8 中已被弃用,因此请改用 UIAlertController 类。

    由于您将“accessoryView”设置为 UIView。您可以使用

    访问该视图
      UIView *myView = [alertView valueForKey:@"accessoryView"]; 
    

    并使用该方法使用标记值查找文本字段

     UITextField * textFeld = [myView viewWithTag:100];
    

    【讨论】:

    • 谢谢,你是对的,我正在使用 UIAlertController,现在效果很好,我遵循了以下问题。 stackoverflow.com/questions/26074475/…
    • 我搜索了很多以获得超过 2 个文本字段的警报视图,上面的 stackoverflow 有它的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多