gpt4 book ai didi

ios - 如何从 textFieldDidEndEditing 获取文本?

转载 作者:行者123 更新时间:2023-12-01 18:10:11 26 4
gpt4 key购买 nike

可能是一个简单的答案,

我怎样才能通过 textField.text来自具有文本字段的 UIAlertView

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
我试图创建一个属性 NSString 并使其等于 textField.text并尝试 NSLog它又回来了NULL

最佳答案

更新的答案

检索 UIAlertView 中文本字段的文本您可以使用 textFieldAtIndex: 方法。您根本不需要涉及文本字段委托(delegate)。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextField *textField = [alertView textFieldAtIndex:0]; // adjust the index if you have more than one text field.

NSLog(@"Text: %@", textField.text);
}

原始答案

您在这里有几个选择。

选项1

将对文本字段的引用存储为属性,您似乎已经在这样做了。
@property (nonatomic, weak) IBOutlet UITextField *textField;

然后可以直接从 UIAlertView 访问。委托(delegate)方法,假设委托(delegate)是引用文本字段的同一个对象:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Text: %@", self.textField.text);
}

选项 2

如果由于某种原因您没有对文本字段的引用,您可以从 textFieldDidEndEditing: 获取它。委托(delegate)方法,首先创建一个属性来存储文本:
@property (nonatomic, copy) NSString *textFieldText;

然后在调用文本字段委托(delegate)方法时更新它。
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.textFieldText = textField.text;
}

然后您可以在 UIAlertView 中访问此属性委托(delegate)方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Text: %@", self.textFieldText);
}

关于ios - 如何从 textFieldDidEndEditing 获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32946003/

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