1

我创建了一个UITextField被调用textField的、一个被调用NSArray的字符串keywordsArray和一个提交按钮。

我想要发生的是,如果用户的文本输入UITextField包含其中的一个字符串,keywordsArray它将推送到一个视图控制器,如果不是,它将推送到另一个。

现在我的代码如下所示:

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

但后来它说

“不兼容的指针类型将'NSArray *'发送到'NSString *'类型的参数”

我如何做到这一点,以便如果有人输入“他很高”,它会推送到,YesViewController因为“高”是一个关键字。

4

3 回答 3

2

这一行:

if ([self.textField.text isEqualToString:self.keywordArray]) 

是你的错误所在。基本上,将字符串与数组进行比较没有任何意义。

而不是你正在做的事情,你应该检查text来自文本字段的是否包含数组中的任何单词。这意味着遍历数组并检查每个项目。

这可能看起来像:

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;
于 2013-10-22T22:14:25.087 回答
1

基本上,我同意 Wain 的部分代码

for (NSString *word in self.keywordArray) {
    if ([self.textField.text rangeOfString:word].location != NSNotFound) {
 ...

但是在 BOOL 函数中推送视图控制器是非常错误的。我会这样写你的方法:

// 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;
}
于 2013-10-22T23:07:48.373 回答
0
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];
 }
于 2013-10-31T05:48:14.987 回答