2017-07-25 21 views
0

我已經創建了一個actionSheet,其中包含3個選項,可以推送到其他視圖控制器並調用一個數字。該選項位於我的應用程序的每個屏幕上的導航欄上的按鈕上。我試圖找到一種重用代碼的方式,而不必爲每個屏幕重新輸入代碼。我知道有一種方法可以通過子類化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) 
} 
} 

,我發現了錯誤:

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

同時在followAction和shopAction線和「型‘moreButton’的值沒有任何成員‘存在’ 「在最後一行。當我直接從按鈕創建IBAction時,此代碼正在工作,但現在它似乎有錯誤,我不知道如何糾正。

+0

你試圖讓一個按鈕做適合視圖控制器業務的工作。視圖控制器可以執行segues並顯示其他控制器,但按鈕不能,因此'self'不知道你要求什麼。考慮將這些代碼放在'UIViewController'的子類中,然後讓所有其他「屏幕」從該VC繼承。 –

+0

這對了解真的很有幫助。我只是假設,因爲它從一個按鈕的IBAction,它會工作,如果我的子類按鈕。我現在正在從下面的代碼中看到幾個問題,但我會看看它是否可行 – Sprokalero

+0

這個想法是,IBAction是按鈕代表它發生的事情,而不是按鈕本身的作用。 –

回答

0

你會得到這些錯誤,因爲performSeguepresent是UIViewController方法。

而不是繼承UIButton,我會創建一個新的助手類,用類方法爲您構建UIAlertController。該方法將接受UIViewController作爲其參數,以便它可以正確構建UIAlertAction。

你會從displayActionSheet喜歡把這種新的方法:

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

適應你的創作代碼粘貼到新的類會出來這樣的:

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

這部分工作。我最初把alertController放在viewDidLoad中,所以我現在已經移動了,但是我收到錯誤:「無法將類型'(NSObject) - >() - >'displayActionSheet'的值轉換爲期望的參數類型'UIViewController'」 。我現在也在接收「預期聲明」(alert ....) – Sprokalero