2017-07-25 8 views
0

他のビューコントローラを押して番号を呼び出す3つのオプションを持つactionSheetを作成しました。このオプションは、アプリの各画面のナビゲーションバーにあるボタンにあります。私は文字通りすべての画面でそれをタイプすることなくコードを再利用する方法を見つけようとしています。私はUIButtonをサブクラス化することによってこれを行う方法があると理解していますが、私は数日間検索して、実際にサブクラスをコード化する方法について決定的な答えを見つけることはできません。今の私のコードは次のとおりです。Xcodeの再利用可能な*ボタン*

class moreButton: UIButton { 

@IBAction func displayActionSheet(_ sender: Any) { 
    let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet) 
     let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "moveSegue", sender: self) 
    }) 
     let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "shopSegue", sender: self) 
    }) 
     let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in 
      let url:URL = URL(string: "tel://number")! 
      UIApplication.shared.open(url, options: [:], completionHandler: nil) 

    } 

    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

    alertController.addAction(followAction) 
    alertController.addAction(shopAction) 
    alertController.addAction(callAction) 
    alertController.addAction(cancel) 


    self.present(alertController, animated: true, completion: nil) 
} 
} 

私はエラーになっています:followActionとshopActionラインの両方で

value of type 'moreButton' has no member 'performSegue'

をし、「タイプの値が 『moreButtonが』いいえメンバー 『が存在』を持っていません"最後の行に。このコードは、ボタンから直接IBActionを作成したときに機能していましたが、今修正する方法がわからないというエラーがあるようです。

+0

あなたはボタンを作るしようとしている:のようなものを出してくるだろう、新しいクラスにあなたの作成コードを適応さ

let alertController = ActionSheetBuilder.build(viewController: self) present(alert, animated: true, completion: nil) 

あなたのようなdisplayActionSheetからこの新しいメソッドを呼びたいですビュー・コントローラーのビジネスを適切に行うことができます。 Viewコントローラはセグを実行して他のコントローラを表示することはできますが、ボタンはできません。したがって、 'self'はあなたが何を求めているのか分かりません。このコードを 'UIViewController'のサブクラスに入れて、他のすべての"スクリーン "をそのVCから継承させることを検討してください。 –

+0

これは本当に知っていると便利です。私はボタンをサブクラス化すると動作するボタンからIBActionを使用していたため、これを仮定しました。私は今、下のコードからいくつかの問題を実行していますが、それが動作するかどうかを確認します – Sprokalero

+0

アイデアは、IBActionはボタンがそのために発生するものであり、ボタン自体が行うものではありません。 –

答えて

0

performSeguepresentはUIViewControllerメソッドなので、これらのエラーが発生しています。

UIButtonをサブクラス化する代わりに、新しいヘルパークラスを作成して、UIAlertControllerを構築するクラスメソッドを作成します。このメソッドはUIAlertActionsを正しく構築できるように、UIViewControllerをパラメータとして受け取ります。

final class ActionSheetBuilder { 
    class func build(viewController: UIViewController) -> UIAlertController { 
     let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet) 
     let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "moveSegue", sender: viewController) }) 
     let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "shopSegue", sender: viewController) }) 
     let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in 
      let url:URL = URL(string: "tel://2845471035")! 
      UIApplication.shared.open(url, options: [:], completionHandler: nil) 
     } 
     let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 

     alertController.addAction(followAction) 
     alertController.addAction(shopAction) 
     alertController.addAction(callAction) 
     alertController.addAction(cancel) 

     return alertController 
    } 
} 
+0

これは部分的に働いた。私はもともとviewControlをviewDidLoad内に配置していたので、今は移動しましたが、エラーを受け取りました: "予想通りの引数型 'UIViewController'に 'type'(NSObject) - >() - > 'displayActionSheet' 。私はまた、現在(アラート....)の行に '期待される宣言'を受け取っています – Sprokalero

関連する問題