2015-04-12 40 views
-2

我从我的添加栏按钮项下面的代码中调用了我提供的警报视图,询问用户输入。它工作正常,第一次,并给后出现以下错误:UIAlertController错误

代码:

var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert) 
    func userBlogLinkEntryPopover() { 
     //  let alert = UIAlertView(title: "Enter Blog Link", message: nil, delegate: self, cancelButtonTitle: "Cancel") 
     alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      textField.placeholder = "Enter Blog URL!" 
     } 
     alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 
      if let tf = self.alert.textFields?.first as? UITextField{ 
       println(tf.text) 
      } 
     })) 
     alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
     presentViewController(alert, animated: true, completion: nil) 

    } 

错误:“UIAlertController只能用 UIAlertActionStyleCancel风格一个动作”

我认为每次按下添加按钮时都会尝试添加操作,因此会出现错误。请纠正我,如果我错了,也请建议工作。

感谢您的帮助。

+1

为什么您将'var alert'声明从函数'userBlogLinkEntryPopover'移出?把它放回去。警报不需要是属性。 – matt

回答

1

我发现,因为alert是在函数外部声明的,所以它保留了所有的操作,并且因为它引发了异常。我更正了我的代码,如下所示,它工作正常。

func userBlogLinkEntryPopover() {  
     var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert) 
     alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
      textField.placeholder = "Enter Blog URL!" 
     } 
     alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in 
      if let tf = alert.textFields?.first as? UITextField{ 
       println(tf.text) 
      } 
     })) 
     alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
     presentViewController(alert, animated: true, completion: nil) 

    } 

问候

0

你也可以得到这个,如果你做到这一点,请仔细阅读看看你是否能看到错误

resendAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in 
     //cancel clicked 
     return 
    })) 

    resendAlert.addAction(UIAlertAction(title: "Check Server", style: .cancel, handler: { (action: UIAlertAction!) in 
     //check server clicked 
     return 
    })) 

注意--->的.cancel是使用TWICE它需要在第二个.default

resendAlert.addAction(UIAlertAction(title: "Check Server", style: .default, handler: { (action: UIAlertAction!) in 
     //check server clicked 
     return 
    })) 

正常工作

相关问题