ViewController.swiftに記載のボタンアクションで、別ファイル(ShareManager.swift)に記載したクラスからアクションシートを表示させ、シートをタップするとFacebook、Twitterをシェアするためのダイアログを表示させようとしているのですが、それが表示されません。
アクションシートを表示さるところまではうまく行ってます。
Swift
1// ViewController.swift 2 3import UIKit 4import Social 5 6class ViewController: UIViewController { 7 @IBAction func btnShowAction(sender: AnyObject) { 8 let showAction = shareManager().showActionSheet() 9 presentViewController(showAction as! UIViewController, animated: true, completion: nil) 10 } 11 12 func showSNS(i: Int) { 13 let dialog = shareManager().setSNS(i) 14 // print(dialog)は返ってきてる 15 presentViewController(dialog as! UIViewController, animated: true, completion: nil) 16 } 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 } 20}
Swift
1// ShareManager.swift 2 3import UIKit 4import Social 5 6class shareManager { 7 func showActionSheet() -> AnyObject { 8 let actionSheet = UIAlertController() 9 actionSheet.addAction( 10 UIAlertAction(title: "Facebook", style: .Default, handler: {(action) -> Void in 11 ViewController().showSNS(0) 12 }) 13 ) 14 actionSheet.addAction( 15 UIAlertAction(title: "Twitter", style: .Default, handler: {(action) -> Void in 16 ViewController().showSNS(1) 17 }) 18 ) 19 actionSheet.addAction( 20 UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil) 21 ) 22 return actionSheet 23 } 24 25 func setSNS(i: Int) -> AnyObject { 26 var dialog = SLComposeViewController() 27 switch i { 28 case 0: 29 dialog = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 30 case 1: 31 dialog = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 32 default: 33 break 34 } 35 dialog.setInitialText("共有するテキスト") 36 return dialog 37 } 38}
下記のWarningが出てたのでネットで検索して
Warning: Attempt to present <SLComposeViewController: 0x1769f2f0> on <test.ViewController: 0x176dc240> whose view is not in the window hierarchy!
表示させるタイミングなのかと思って遅延処理を入れてみたのですが、それでもダメでした。
Swift
1// ViewController.swift 2 let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))) 3 dispatch_after(delayTime, dispatch_get_main_queue()) { 4 self.presentViewController(dialog as! UIViewController, animated: true, completion: nil) 5 }
環境
Xcode 7.3
Swift 2.2
まだまだ理解できていないこともありますが、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/13 23:33