swift - 从 View Controller 调用函数 - 参数错误 - Swift2

标签 swift swift2

我在调用 tweet 函数时遇到问题。我不断收到参数错误。我也尝试了 (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)
 }
}

最佳答案

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

vc.tweetAction(self)

关于swift - 从 View Controller 调用函数 - 参数错误 - Swift2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37823140/

相关文章:

ios - 如何修复 C 风格的语句?

ios - Facebook 注销按钮(iOS SDK)在注销时适当的 segue

swift - 在哪里快速实现 crashlytics 的 setUserIdentifier 函数

ios - Swift:不能用类型索引下标 [String:NSObject] 的值?

ios - 如何自动调整单元格大小以适合每行 4 个单元格

ios - 无法从 Cocoapods 渲染 SwiftUI 预览

ios - Xcode8 : Why Xcode8 is not building/compiling after converting to Swift3 syntax's

ios - 将 UITableViewCell 滚动到 tableview 的顶部

ios - 前缀在 Xcode 7 beta 6 中是否正常工作?

ios - Swift 中的 "func observeValueForKeyPath(keyPath : NSString, object : AnyObject, change : NSDictionary, context : Void)"问题