2015-11-09 34 views
1

我建立一個應用程序與UIAlertController,我想有一個按鈕,上面寫着「共享」,然後打開了類似添加共享動作的UIAlertController斯威夫特

this

似乎容易做,如果它不是一個子按鈕。

我的代碼

} 


@IBAction func buttonTapped(sender: UIButton) { 

    var title: String 

    if sender.tag == correctAnswer { 
     title = "Correct" 
     ++score 
    } else { 
     title = "Wrong" 

     let ac = UIAlertController(title: title, message: "Your score was \(score).", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "Try Again", style: .Default, handler: askQuestion)) 
     ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: share)) 
     presentViewController(ac, animated: true, completion: nil) 
     gameOver = true 
     score = 0 

    } 

我的共享功能目前尚未確定。

+0

你在哪裏定義您完成處理程序爲您份額的行動? – pbush25

回答

0

在iPhone做這樣的事情,如果你的share方法不起作用

ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: { (action) -> Void in 
    // Do the Facebook sharing here 
    let content = FBSDKSharePhotoContent() 
    content.photo = .... 
    .... 
})) 
+0

我明白了。我明白你爲什麼有點困惑。這只是一個例子,我只想提一些預先填好的文字。 –

+0

不知道你究竟想要什麼。如何只是Facebook的API?或者只是將分享按鈕操作與彈出窗口鏈接起來?或者實際上建立自己的popupView? – zcui93

1

您可以在您完成處理程序的UIAlertAction後打開UIActivityController定義被竊聽,但默認情況下沒有蘋果展示你想要的份額,在iPad是。用下面的代碼就可以顯示該按鈕被點擊後的份額:

let message = "The message" 
let alertController = UIAlertController(title: "The title", message: message, preferredStyle: .Alert) 

let shareAction = UIAlertAction(title: "Share", style: .Default, handler: { x -> Void in 

    let firstActivityItem = "Text you want" 
    let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")! 

    // If you want to put an image 
    let image : UIImage = UIImage(named: "image.jpg")! 

    let activityViewController : UIActivityViewController = UIActivityViewController(
       activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil) 

    // Anything you want to exclude 
    activityViewController.excludedActivityTypes = [ 
      UIActivityTypePostToWeibo, 
      UIActivityTypePrint, 
      UIActivityTypeAssignToContact, 
      UIActivityTypeSaveToCameraRoll, 
      UIActivityTypeAddToReadingList, 
      UIActivityTypePostToFlickr, 
      UIActivityTypePostToVimeo, 
      UIActivityTypePostToTencentWeibo 
     ] 

     self.presentViewController(activityViewController, animated: true, completion: nil) 
}) 

alertController.addAction(shareAction) 
self.presentViewController(alertController, animated: true, completion: nil) 

在上面的代碼中,我總結了添加更多按鈕,我假設你想爲iPhone的代碼,如果你想它也爲iPad的你可以檢查這個問題,我的答案:

我希望這可以幫助您。

0

這裏有一個答案: 我定義的類份額爲:

FUNC份額(動作:UIAlertAction){

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) { 
     let facebook:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
     facebook.setInitialText("I just scored \(score) in Flagpoles.") 

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

     score = 0 

    } else { 
     let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert) 

     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alert, animated: true, completion: nil) 
    }