消除重复的代码Swift 3

时间:2017-02-13 09:06:45

标签: ios swift uinavigationcontroller uinavigationbar

我的应用中有多个导航控制器及其根视图控制器。我希望每个导航栏都有紧密放置在栏右侧的社交媒体按钮。为此,我使用此代码显示1个视图控制器中的按钮:

    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新手。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

大多数重复都是通过使用函数来解决的。第一步是将该代码提取到一个函数中,第二步是从多个位置使用相同的函数。

您可以将其添加到扩展程序,例如:

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

并且只能打电话

self.addShareButtons()

来自需要按钮的每个控制器。

您也可以将按钮处理程序添加到扩展程序中。

另一种方法是使用UIViewController子类,但如果你想使用UITableViewController子类,这总是一个问题。

答案 1 :(得分:0)

您可以设计自定义导航控制器并将这些代码添加到导航控制器。将导航控制器继承到故事板或以编程方式继承您想要使用的位置。

//示例代码

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

答案 2 :(得分: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)使用您想要使用的UIBarButtonItem创建自定义UINavigationController,并重用该控制器,和/或2)创建如下协议:

protocol FacebookBtnHandler {
    func fbBtnPressed()
}

......并且任何相关的VC都符合它。这将允许您在FacebookButton init方法中为按钮分配目标和选择器,您可以在其中分配图像和帧,从而防止重复该行。

答案 3 :(得分:0)

尝试通过创建UIBarButton

的子类来实现bar按钮
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 
     }
}