斯威夫特 - 执行塞格

Fab*_*gue 27 self ios swift

if (view.annotation.title as String!) == "Helgoland " {
  currentLong = 7.889021
  currentLat = 54.180210 
  url = "google.com"

  let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
  let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
  let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
    performSegueWithIdentifier("showShop", sender: self)
  })
  let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
    youtubeVideoID = "XX"
    UIApplication.sharedApplication().openURL(NSURL(string: "http://www.youtube.com/watch?v=\(youtubeVideoID)"))
  })
  alertController.addAction(cancelAction)
  alertController.addAction(button1action)
  alertController.addAction(button2action)
  self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

我总是得到一个错误

"关闭时隐式使用'自我';使用'自我'.使捕获语义显式"

但如果我设置self.view,它也会失败.

Yam*_*man 78

你需要明确地使用self:

self.performSegueWithIdentifier("showShop", sender: self)

而对于Swift 3(thx @KingChintz):

self.performSegue(withIdentifier: "showShop", sender: self)

  • 在Swift3中,相当于:```self.performSegue(withIdentifier:"theID",sender:self)``` (2认同)