从viewcontroller调用函数 - 参数错误 - Swift2

时间:2016-06-14 22:26:16

标签: swift swift2

我无法调用推文功能。我一直在弄错参数。我也试过(AnyObject)并得到了 错误:参数类型不符合预期...

我是swift的新手,不知道如何让它运行起来。尝试了我能想到的一切。谢谢

   // from GameScene 
  var vc = ViewController()
  vc.tweetAction(sender: AnyObject)
  //error: cannot create single-element tuple with an element label



   //function in View Controller below

    @IBAction func tweetAction(sender: AnyObject){

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){

        let tweetController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)

        tweetController.setInitialText("I Scored on this app")

        self.presentViewController(tweetController, animated: true, completion: nil)
    }
    else{

        let alert = UIAlertController(title: "Accounts", message: "Please log into your twitter to share", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))

        alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default, handler: { (UIAlertACtion) in

            let settingsURL = NSURL(string:UIApplicationOpenSettingsURLString)

            if let url = settingsURL{

                UIApplication.sharedApplication().openURL(url)
            }
        }))
    self.presentViewController(alert, animated: true, completion: nil)
 }
}

2 个答案:

答案 0 :(得分:0)

您正在将类型传递给方法调用而不是实例。您还包含第一个参数的标签,该标签不正确。尝试:

vc.tweetAction(self)

答案 1 :(得分:0)

尝试将发件人参数类型从AnyObject更改为UILabel

 var vc = ViewController()
 vc.tweetAction(yourUILabelInstance)

并且不要忘记修改tweetAction功能

@IBAction func tweetAction(sender: UILabel){
...
}