简体   繁体   English

当使用NSArray中的字符串时,如何将UITextField推送到新的ViewController?

[英]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. 我创建了一个名为textFieldUITextField ,一个名为keywordsArrayNSArray字符串和一个提交按钮。

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.

相关问题 如何关闭并推送新的ViewController - How to dismiss and push new ViewController 如何在新窗口中推送 ViewController - How to Push ViewController in new window 单击 tableView 单元格时如何推送新的 viewController? - How to push a new viewController when clicking on a tableView cell? 轻按单元格时将值推送到新的ViewController - Push the values to new ViewController when a cell is tapped 如何推送具有透明背景的新viewController? - How to push a new viewController with Transparent Background? 调用时如何在viewController中保留我的uitextfield值? - how to retain my uitextfield values in viewController when i calling? 当在 Swift 5 中点击 Tableview Row 时,如何使用 Xib(不是 StoryBoards)在 SideMenu Controller 中设置 UINavigationController 以推送新的 ViewController - How to set UINavigationController in SideMenu Controller using Xib (Not StoryBoards) to push new ViewController when Tableview Row Tapped in Swift 5 推送到另一个ViewController时如何隐藏UINavigationBar? - How to hide UINavigationBar when push to another ViewController? 实现didReceiveRemoteNotification以在推送到来时显示一个新的viewController - Implement didReceiveRemoteNotification to show a new viewController when push comes 从connectionDidFinishLoading推送到新的ViewController - Push to new ViewController from connectionDidFinishLoading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM