2016-06-14 69 views
0

我無法調用推文函數。我不斷收到錯誤的參數。我也試過(AnyObject),並得到 錯誤:參數類型不符合預期...從視圖控制器調用函數 - 參數錯誤 - Swift2

我是新來的迅速,不知道如何得到這個運行。嘗試了我能想到的一切。謝謝

// 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) 
} 
} 

回答

0

您傳遞一個類型到您的方法調用,而不是一個實例。您還包括第一個參數的標籤,這是不正確的。嘗試:

vc.tweetAction(self) 
+0

自我似乎在破解密碼按鈕。該行---------------->讓skView = self.view as! SKView拋出錯誤「Thread1信號SIGARBT, –

+0

什麼是錯誤?是否有堆棧跟蹤? –

+0

錯誤」線程1:信號SIGABRT「斷開的線是'let skView = self.view as!SKView',我配置view。控制檯顯示'無法將類型UIVIew的值轉換爲SKView,如果這個按鈕是一個SKSpriteNode,那麼這個按鈕就是一個SKSpriteNode –

0

嘗試發送參數類型從AnyObject改變UILabel

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

而且不要忘記修改tweetAction功能以及

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