我收到一个非常奇怪的错误。我认为编译器试图告诉我它不能转到另一个视图控制器,直到它完成执行当前视图控制器中的所有代码,但我我不确定。
我通过使用警告框(即调用名为generateTextField
的函数)逐字输入。
然后,当我完成时,我说"嘿,我想让你去另一个视图控制器" - 但是编译器反而告诉我"嘿,我不这么认为"。
这是我的错误:
警告:尝试呈现HairStyle1ViewController:0x7 ...>在browseBarbersViewController上:0x7 ...>已经呈现 警告:尝试呈现HairStyle1ViewController:0x7 ..>在browseBarbersViewController上:0x7 ...>已经提交
@IBAction func AddNewStyleButtonClicked(sender: AnyObject)
{
// Get the "hairstyle name" from the user
generateTextField();
// OK We are done with that function, now transition to the
// next screen
performSegueWithIdentifier("HairStyle1", sender: self);
}
// Generate a text field for user input (i.e. call the alert function)
func generateTextField()
{
//1. Create the alert controller.
var tempStyle = "";
var alert = UIAlertController(title: "Add a New Style", message: "Enter the name of the new hairstyle below", preferredStyle: .Alert);
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.placeholder = "Your New Hairstyle Goes Here..";
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
tempStyle = textField.text!;
print("New Style Added is: " + tempStyle);
HairStyle = tempStyle;
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
}
当我拿出generateTextField()
函数时,它完美地执行了segue,这也很奇怪。我很困惑。
答案 0 :(得分:2)
我通过添加
来解决这个问题self.performSegueWithIdentifier("HairStyle1", sender: self);
之后
HairStyle = tempStyle;
行