2017-02-13 73 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 
    } 
}