swift - 直接从导航 Controller 设置 BarButtonItem

标签 swift swift2

我有一个导航 Controller ,其中有一个名为“LoginController”的 rootViewController。当我尝试从 LoginController 的 init 添加 rightBarButtonItem 时,它可以工作,但直接在导航 Controller 中相同的操作不起作用。

这有效:

class LoginController: UIViewController {

    init(){
        super.init(nibName: nil, bundle: nil)

        let button = UIBarButtonItem()
        button.title = "Test2"
        navigationItem.rightBarButtonItem = button        
    }

    ......

}

这不起作用:

class MainNavigationController : UINavigationController{

    private var _distributionProvider : DistributionProvider!

    init(rootViewController: UIViewController, distributionProvider : DistributionProvider) {
        _distributionProvider = distributionProvider
        super.init(rootViewController: rootViewController)

        navigationBar.barTintColor = UIColor(red: 90/255, green: 177/255, blue: 225/255, alpha: 1)

        let button = UIBarButtonItem()
        button.title = "Test"
        navigationItem.rightBarButtonItem = button


    }


   .... 
}

问题出在哪里?谢谢

最佳答案

你必须这样做:

class MainNavigationController: UITabBarController, UITabBarControllerDelegate{
 override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }

    override func viewWillAppear(animated: Bool) {
        let item1 = Item1ViewController()
        let icon1 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
        item1.tabBarItem = icon1
        let controllers = [item1]  //array of the root view controllers displayed by the tab bar interface
        self.viewControllers = controllers
    }

    //Delegate methods
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
        print("Should select viewController: \(viewController.title) ?")
        return true;
    }

}

关于swift - 直接从导航 Controller 设置 BarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37865319/

相关文章:

ios - 动态宽度大小以适应 stackview 中的控件

swift2 - 模块 'Firebase' 没有名为 'defaultConfig' 的成员

ios - 从坐标数组到表中的位置之间的距离

ios - 从 TextBox 到 Widget 的文本?

ios - 为核心数据一对多关系添加一个项目到 NSSet

ios - Swift 2.0 Defer 在 XCode 7.0.1 中不工作?

ios - 在 CNContactPickerViewController 中隐藏或禁用搜索栏

uitableview - Swift UITableView 在视觉上取消选择滚动上的单元格

ios - 修改多张照片时只有一个 iOS 权限对话框

ios - ( swift )PrepareForSegue : fatal error: unexpectedly found nil while unwrapping an Optional value