我已经初始化了两个UITextField
,tf
和tf1
。视图加载时,我希望两个字段的文本分别为_minPrice
和_maxPrice
。这是两个NSString属性,它们是从以前的ViewController获取其值的。当当前ViewController加载时,它会显示正确的值,因此我知道这部分工作正常。
我希望用户能够编辑tf
和tf1
的内容,以便将这两个UITextField
中包含的任何内容用作下面的editMatchCenter
函数中的参数,无论该字符串是否为来自_minPrice
和_maxPrice
,或用户输入的替代文本。编辑字段可以正常工作,但是当我按下Submit按钮运行editMatchCenter
时,什么也没发生,这告诉我self.tf.text和self.tf1.text为空是出于某种原因。
我尝试注释掉if语句,但出现崩溃提示:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
指的是@"minPrice": self.tf.text
。
文本字段初始化:
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 125, 70, 30)];
[self.view addSubview:tf];
tf.userInteractionEnabled = YES;
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text = _minPrice;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(195, 125, 70, 30)];
[self.view addSubview:tf1];
tf1.userInteractionEnabled = YES;
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text = _maxPrice;
提交按钮功能:
- (IBAction)submitButton:(id)sender
{
if (self.tf.text.length > 0 && self.tf1.text.length > 0) {
[PFCloud callFunctionInBackground:@"editMatchCenter"
withParameters:@{
@"searchTerm": _searchTerm,
@"minPrice": self.tf.text,
@"maxPrice": self.tf1.text,
@"itemCondition": _itemCondition,
@"itemLocation": _itemLocation
}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(@"Result: '%@'", result);
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
}
}
标头中的属性:
@property (strong, nonatomic) NSString *minPrice;
@property (strong, nonatomic) NSString *maxPrice;
@property (strong, nonatomic) NSString *itemCondition;
@property (strong, nonatomic) NSString *itemLocation;
@property (strong, nonatomic) NSString *searchTerm;
@property (strong, nonatomic) IBOutlet UITextField *tf;
@property (strong, nonatomic) IBOutlet UITextField *tf1;
最佳答案
得到零的原因是因为您从未初始化属性。而是在代码(tf和tf1)中创建新的指针。
尝试用self.tf替换tf并用self.tf1替换tf1。像这样:
self.tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 125, 70, 30)];
self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(195, 125, 70, 30)];
关于ios - UITextField没有在函数中注册任何文本长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26091160/