1

UITextFieldと呼ばれるtextField、というNSArray文字列のkeywordsArray、および送信ボタンを作成しました。

私が望んでいるのは、ユーザーのテキスト入力にUITextField文字列の 1 つが含まれて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

1

基本的に、コードのウェインの部分に同意します

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

しかし、BOOL関数でView Controllerをプッシュするのは非常に間違っています。私はあなたの方法を次のように書きます:

// 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 に答える