如果输入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语句的情况下工作正常,所以我不确定是什么错误:/
出现问题的是当我单击“提交”时,当我将“Hello”显示在黑屏中时显示没有错误消息,而不是显示正确的视图
提前致谢!
答案 0 :(得分:0)
我认为您要将字符串与小写字母进行比较,可能是您的文字字段值"hello"
不等于"Hello"
。所以请比较
if(TextField.text!.caseInsensitiveCompare("Hello") == NSComparisonResult.OrderedSame)
{
// do your stuff
}
else{
print(TextField.text)
}
希望它会对你有所帮助。