当没有数据时,不执行动作

时间:2016-02-07 12:53:40

标签: ios swift

点击按钮后,我有一个操作打开网址,我想知道如何对其进行编程,以便在Facebook = "0"时出现UIAlertController

更新并正常运行的代码是:

let action2 = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default) { (action) in

   if self.facebook == "0" {

     let alert = UIAlertController(title: "Lo sentimos", message: "La cofradía no dispone de Facebook, inténtalo a través de otro medio", preferredStyle: UIAlertControllerStyle.Alert)

let action1 = UIAlertAction(title: "Atrás", style: UIAlertActionStyle.Cancel, handler: nil)

       alert.addAction(action1)
       self.presentViewController(alert, animated: true, completion: nil);

        } else {

          UIApplication.sharedApplication().openURL(NSURL(string: "http://facebook.com/\(self.facebook)")!)

 }

1 个答案:

答案 0 :(得分:1)

在打开网址之前,只需检查它是否为空:

// I'm assuming facebook is a String that is empty if it isn't set

if facebook != "" {
    UIApplication.sharedApplication().openURL(NSURL(string: "http://facebook.com/\(self.facebook)")!)
} else {
    // Open alertcontroller here
}