2016-04-04 47 views
-1
@IBAction func createAlert(sender: AnyObject) { 

    if #available(iOS 8.0, *) { 
     let alert = UIAlertController(title: "Hey there!", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.Alert) 
    } else { 

     if let alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in 

     self.dismissViewControllerAnimated(true, completion: nil) 

     })) 
    } 


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

    } 

我不斷收到錯誤消息。 1)'if'條件後的'{' 2)條件中的變量綁定需要一個初始化器。 請幫忙!!!!創建UIAlertController時出錯

+1

更新Xcode。你的不認識'#可用(iOS 8.0,*)'。 – Moritz

+0

'如果讓alert#'在它後面沒有任何阻止。如果甚至需要? –

+0

如果讓alert.addAction(...沒有打開'{' – pkacprzak

回答

2

你有很多的語法錯誤。您不需要將alert.addAction換成if-let。另外請確保您的ifelse陳述與您想要做的事情相符。我不確定你想要完成什麼,但我猜測你想要顯示一個UIAlertController如果當前設備運行的是iOS 8或更高版本,請使用運行iOS 7及更低版本的設備執行某些操作:

class VC : UIViewController { 
    @IBAction func createAlert(sender: AnyObject) { 

     if #available(iOS 8.0, *) { 
      let alert = UIAlertController(title: "Hey there!", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 

      self.presentViewController(alert, animated: true, completion: nil) 
     } else { 
      // handle iOS 7 case 
      // set up a UIAlertView, etc 
     } 
    } 
} 
+0

你實際上不需要使用alert controllers來調用dismissViewController,而且如果你使用尾隨閉包,這段代碼看起來可能會稍微現代一點 – crashoverride777

+0

謝謝你的工作。這,我正在上一門課程,他的代碼不適合我的版本,即時猜測 –