Swift:继承导航栏类

时间:2016-08-18 04:30:53

标签: ios swift

我有一个CustomNavBarController类,基本上可以完成与导航栏相关的所有操作。它将所有按钮添加到导航栏并处理所有功能。

截至目前,我继承了这个类:

class SomeController: CustomNavBarController

它工作正常,但它目前阻止了我想用控制器做的一些事情,比如添加集合视图。

CustomNavBarController.swift (没有函数)

class CustomNavBarController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    func addButtonsToNavBar() {

        let homeButton = UIButton(type: UIButtonType.System)
        homeButton.frame = CGRectMake(0, 0, 30, 30)
        homeButton.addTarget(self, action: #selector(tappedHome), forControlEvents: .TouchUpInside)
        homeButton.setImage(UIImage(named: "icon_home"), forState: .Normal)
        let homeBarButtonItem = UIBarButtonItem(customView: homeButton)

        let messageButton = UIButton(type: UIButtonType.System)
        messageButton.frame = CGRectMake(0, 0, 30, 30)
        messageButton.addTarget(self, action: #selector(tappedMessage), forControlEvents: .TouchUpInside)
        messageButton.setImage(UIImage(named: "icon_message"), forState: .Normal)
        let messageBarButtonItem = UIBarButtonItem(customView: messageButton)

        let userButton = UIButton(type: UIButtonType.System)
        userButton.frame = CGRectMake(0, 0, 30, 30)
        userButton.addTarget(self, action: #selector(tappedProfile), forControlEvents: .TouchUpInside)
        userButton.setImage(UIImage(named: "icon_user"), forState: .Normal)
        let userBarButtonItem = UIBarButtonItem(customView: userButton)

        navigationItem.rightBarButtonItems = [userBarButtonItem, messageBarButtonItem, homeBarButtonItem]
    }

    view.backgroundColor = Constants.MAIN_BACKGROUND_COLOR

    addButtonsToNavBar()
    }
}

MainController.swift

let cellId = "cellId"

class NewsController: UICollectionViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)

    collectionView?.registerClass(FeedCell.self, forCellWithReuseIdentifier: cellId)
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    return collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(view.frame.width, 50)
    }
}

class FeedCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupViews() {
    backgroundColor = UIColor.whiteColor()
}
}

// Load navigation bar items
class CustomNavBarController: NewsController {

}

在我的MainController.swift文件中,我试图通过子类化来加载导航栏项。由于多重继承,将它添加到NewsController不再起作用。

为了做到这一点,我应该先将其改为公开,还是有不同的方法来实现这一目标?

0 个答案:

没有答案