2013-10-22 18 views
1

我創建了一個名爲textFieldUITextField,一個名爲keywordsArray的字符串NSArray和一個提交按鈕。如何在使用NSArray中的字符串時將UITextField推送到新的ViewController?

我想要發生的事情是,如果用戶輸入到UITextField的文本包含keywordsArray中的一個字符串,它將推送到一個視圖控制器,否則將推送到另一個視圖控制器。

現在我的代碼如下所示:

[email protected][@"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,因爲「高」是一個關鍵字。

回答

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; 
+0

附:如果你這樣做,你可能想要使用'rangeOfString:options:'來指定不區分大小寫的範圍檢測。 – Wain

+0

當我執行此操作時它崩潰了。我只是把它換成了我的if語句是我做錯了,還是除了這個之外還要添加其他東西? – rivtracks

+0

什麼是崩潰?我再次檢查您的方法後做了更改... – Wain

1

基本上,我同意的代碼

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 
[email protected][@"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; 
} 
+0

它不能識別包含關鍵字的方法: – rivtracks

0
[email protected][@"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]; 
} 
相關問題