【问题标题】:UIButton if statement swiftUIButton if 语句 swift
【发布时间】:2023-03-08 18:59:01
【问题描述】:

如果键入 Hello 一词,我希望按钮转到 QaController,否则我希望出现警报,然后返回主屏幕 (viewController),但它似乎不起作用

@IBAction func submitButton(sender: AnyObject) {
    if TextField.text.containsString("Hello"){
        let secondViewController:QaController = QaController()
        self.presentViewController(secondViewController, animated: true, completion: nil)
    } else {
        let alertController = UIAlertController(title: "Thank You!", message:
            "We appreciate your feedback!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        let homeViewController:ViewController = ViewController()
        self.presentViewController(homeViewController, animated: true, completion: nil)
    }

}

代码在没有 IF 语句的情况下可以正常工作,所以我不确定出了什么问题:/

问题是当我点击提交时,当我输入“你好”时,它显示一个没有错误消息的黑屏,而不是显示正确的视图

提前致谢!

【问题讨论】:

  • but it doesn't seem to work 这是什么意思:是否有错误消息,如果是,是哪一个?预期会发生什么,反而发生了什么?请始终提供上下文和详细信息。谢谢! :)
  • 您在使用情节提要吗?如果是这样,您需要从情节提要中实例化:storyBoard.instantiateViewControllerWithIdentifier(CONTROLLERIDENTIFIERINSTORYBOARDASSTRING)。您可能正在创建一个没有 UI 的控制器。
  • 首先检查您的文本字段是否有内容,使用打印。

标签: ios swift uibutton


【解决方案1】:

我认为您将字符串与小写进行比较,可能是您的文本字段值是"hello" 不等于"Hello"。所以请比较喜欢

if(TextField.text!.caseInsensitiveCompare("Hello") == NSComparisonResult.OrderedSame)
{ 
    // do your stuff
}
else{
        print(TextField.text)
    }

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多