[英]how to have UITextField push to a new ViewController when a string in an NSArray is used?
I have created a UITextField
called textField
, an NSArray
of strings called keywordsArray
, and a submit button. 我创建了一个名为textField
的UITextField
,一个名为keywordsArray
的NSArray
字符串和一个提交按钮。
What I want to have happen is that if the user's text input to the UITextField
contains one of the strings in the keywordsArray
it would push to one view controller, if not it would push to another. 我想要发生的是,如果用户输入到UITextField
的文本包含keywordsArray
中的一个字符串,它将推送到一个视图控制器,否则它将推送到另一个视图控制器。
Right now my code looks like this: 现在我的代码看起来像这样:
self.keywordsArray=@[@"funny, @"tall", @"handsome"];
[submitButton addTarget:self action:@selector(showresponse:) forControlEvents:UIControlEventTouchUpInside];
-(BOOL)showresponse:(UIButton *)sender{
YesViewController *yesViewController=[[YesViewController alloc]init];
NoViewController *noViewController=[[NoViewController alloc]init];
if ([self.textField.text isEqualToString:self.keywordArray]) {
[self.navigationController pushViewController:yesViewController animated:YES];
} else {
[self.navigationController pushViewController:noViewController animated:YES];
}
return YES;
}
but then it says 但随后它说
"Incompatible pointer types sending 'NSArray *' to parameter of type 'NSString *'" “不兼容的指针类型将'NSArray *'发送到'NSString *'类型的参数”
How do i make it so that if someone were to input "he is tall" it would push to YesViewController
because "tall" is a keyword. 如何做到这一点,如果有人输入“他很高”,它会推送到YesViewController
因为“tall”是一个关键字。
This line: 这一行:
if ([self.textField.text isEqualToString:self.keywordArray])
is where your error is. 是你的错误所在。 Basically, it doesn't make any sense to compare a string to an array. 基本上,将字符串与数组进行比较没有任何意义。
Instead of what you're doing you should be checking if the text
from the text field contains any of the words from the array. 相反,你在做什么,你应该检查是否text
从文本字段包含任何从阵列的话。 That means iterating over the array and checking each item. 这意味着迭代数组并检查每个项目。
That might looks something like: 这可能看起来像:
for (NSString *word in self.keywordArray) {
if ([self.textField.text rangeOfString:word].location != NSNotFound) {
[self.navigationController pushViewController:yesViewController animated:YES];
return YES;
}
}
[self.navigationController pushViewController:noViewController animated:YES];
return YES;
Basically, I agree with Wain's part of code 基本上,我同意Wain的代码部分
for (NSString *word in self.keywordArray) {
if ([self.textField.text rangeOfString:word].location != NSNotFound) {
...
But pushing view controller in BOOL function is very wrong. 但是在BOOL函数中推送视图控制器是非常错误的。 I would write your method like this: 我会写这样的方法:
// In your example, you missed to close quotes for word funny
self.keywordsArray=@[@"funny", @"tall", @"handsome"];
[submitButton addTarget:self action:@selector(showresponse:) forControlEvents:UIControlEventTouchUpInside];
-(void)showresponse:(UIButton *)sender{
// Initialize view controller which you'll need, not both.
if ([self containsKeyword:self.textField.text]) {
YesViewController *yesViewController=[[YesViewController alloc]init];
[self.navigationController pushViewController:yesViewController animated:YES];
} else {
NoViewController *noViewController=[[NoViewController alloc]init];
[self.navigationController pushViewController:noViewController animated:YES];
}
}
// Now this method can be reusable for any other field or controler
- (BOOL)containsKeyword:(NSString*)text {
for (NSString *word in self.keywordArray) {
if ([text rangeOfString:word].location != NSNotFound) {
return YES;
}
}
return NO;
}
self.keywordsArray=@[@"funny, @"tall", @"handsome"];
[submitButton addTarget:self action:@selector(showresponse:) forControlEvents:UIControlEventTouchUpInside];
-(BOOL)showresponse:(UIButton *)sender
{
for (NSString *word in self.keywordArray)
{
if ([self.textField.text rangeOfString:word options].location != NSNotFound)
{
YesViewController *yesViewController=[[YesViewController alloc]init];
[self.navigationController pushViewController:yesViewController animated:YES];
return YES;
}
}
NoViewController *noViewController=[[NoViewController alloc]init];
self.navigationController pushViewController:noViewController animated:YES];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.