2017-02-13 71 views
0

我的應用程序中有多個導航控制器及其根視圖控制器。我希望每個導航欄都將社交媒體按鈕緊密地放置在欄的右側。出於同樣的我已經使用這個代碼顯示在1個視圖控制器的按鈕:消除重複的代碼Swift 3

let fbImage = UIImage(named: "Facebook.png")! 
    let twitterImage = UIImage(named: "Twitter.png")! 
    let youtbImage = UIImage(named:"YouTube.png")! 

    let fbBtn: UIButton = UIButton(type: .custom) 
    fbBtn.setImage(fbImage, for: UIControlState.normal) 
    fbBtn.addTarget(self, action: #selector(HomeViewController.fbBtnPressed), for: UIControlEvents.touchUpInside) 
    fbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
    let fbBarBtn = UIBarButtonItem(customView: fbBtn) 

    let twitterBtn: UIButton = UIButton(type: .custom) 
    twitterBtn.setImage(twitterImage, for: UIControlState.normal) 
    twitterBtn.addTarget(self, action: #selector(HomeViewController.twitterBtnPressed), for: UIControlEvents.touchUpInside) 
    twitterBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
    let twitterBarBtn = UIBarButtonItem(customView: twitterBtn) 

    let youtbBtn: UIButton = UIButton(type: .custom) 
    youtbBtn.setImage(youtbImage, for: UIControlState.normal) 
    youtbBtn.addTarget(self, action: #selector(HomeViewController.youtubeBtnPressed), for: UIControlEvents.touchUpInside) 
    youtbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
    let youtbBarBtn = UIBarButtonItem(customView: youtbBtn) 

    self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false) 

現在我想對所有的導航條上的相同鍵。我可以很容易地在每個視圖控制器的viewDidLoad()中複製這些代碼和相應的目標方法,但是太多的代碼正在重複。那麼如何避免這種情況呢?

我正在使用Swift 3.我是iOS新手。任何幫助將不勝感激!

+0

您可以設置從您的UIViewController一個共同的「父母」,如「CommonViewController 「,將該代碼放入其中,並且每個ViewController都將繼承CommonViewController。 – Larme

回答

1

大多數重複是通過使用函數來解決。第一步是將該代碼提取到函數中,第二步是從多個位置使用相同的函數。

您可以將其添加到擴展名,例如:

extension UIViewController { 
    func addShareButtons() { 
     ... 
     self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false) 
    } 
} 

,只有從每一個需要的按鈕控制器調用

self.addShareButtons() 

您也可以將按鈕處理程序添加到擴展。

另一種方法是使用UIViewController子類,但如果要使用UITableViewController子類,則這總是一個問題。

0

您可以設計自定義導航控制器並將這些代碼添加到導航控制器。繼承導航控制器到故事板或以編程方式在您想要使用的位置。

//示例代碼

class "YourNavigationCorollerName": UINavigationController, UINavigationControllerDelegate { 
    override func viewDidLoad() { 
    super.viewDidLoad() 
     //declare here you code, what you want 
    } 
} 
0

假設這些按鈕始終具有相同的行爲,您可以爲它們創建自定義類,並將重複的代碼放置在那裏。例如:

class FacebookButton : UIButton { 

    override init() { 

     super.init() 
     self.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
     self.setImage(UIImage(named: "Facebook.png")!, for: .normal) 
    } 
} 

...我認爲編譯器會罵你初始化一個UIView沒有解碼器和框架,但希望你的想法。

爲了改善這一點,你可以1)創建與您要使用的UIBarButtonItems一個自定義的UINavigationController和重用控制器,和/或2)創建一個協議類似如下:

protocol FacebookBtnHandler { 
    func fbBtnPressed() 
} 

。並且有任何相關的風險投資公司符合它。這將允許您爲FacebookButton init方法中的按鈕指定目標和選擇器,在該方法中分配圖像和幀,從而防止重複該行。

0

通過創建的UIBarButton

class Button: UIBarButtonItem { 
    convenience init(withImage image : UIImage,Target target: Any, andSelector selector: Any?){ 

     let button = UIButton(type: .custom) 
     button.setImage(image, for: UIControlState.normal) 
     button.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
     button.addTarget(target, action: selector, for: UIControlEvents.touchUpInside) 

     self.init(customView: button) 
    } 
} 


let fbImage = UIImage(named: "Facebook.png")! 
let twitterImage = UIImage(named: "Twitter.png")! 
let youtbImage = UIImage(named:"YouTube.png")! 

let fbBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.fbBtnPressed)) 

let twitterBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.twitterBtnPressed)) 

let youtubeBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.youtubeBtnPressed)) 

self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false) 

子類來實現欄按鈕,使之爲所有的視圖控制器

extension UIViewController { 
    func addButtons() { 
     // add above code 
    } 
}