无法从基UIViewController类继承属性

时间:2022-09-24 23:37:36

I'm new to iOS development and this may be a simple question. My application will have many view controllers which will share similar properties. I created a base view controller class which subclasses UIViewController. All other view controllers subclass the base view controllers. However, the properties I set in base view controller class, are not inherited. For example, the navigation bar buttons should all be the color green yet are the default blue color when I build the app.

我是iOS开发的新手,这可能是一个简单的问题。我的应用程序将有许多视图控制器,它们将共享类似的属性。我创建了一个基本视图控制器类,它是UIViewController的子类。所有其他视图控制器都子类化基视图控制器。但是,我在基视图控制器类中设置的属性没有被继承。例如,当我构建应用程序时,导航栏按钮应该都是绿色的,但却是默认的蓝色。

Here's the base view controller class:

这是基本视图控制器类:

class BaseViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.greenColor()
    self.navigationItem.rightBarButtonItem?.tintColor = UIColor.greenColor()
}

This is the code for other view controllers that subclass the base view controller above:

这是上面基视图控制器子类化的其他视图控制器的代码:

class AboutViewController: BaseViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = NSLocalizedString("About", comment: "About page title")
}

I've searched the web and SO for similar questions but wasn't able to find an answer. I don't see anything fundamentally wrong with my code. I am using Xcode 7.2.1 and testing on iOS 8+. Also tested in Xcode 7.2.3 beta but had similar results.

我在网上搜索了类似的问题,但是找不到答案。我看不出我的代码有什么本质上的错误。我正在使用Xcode 7.2.1并在ios8 +上进行测试。在Xcode 7.2.3 beta中也进行了测试,但结果类似。

1 个解决方案

#1


1  

From what I see, this isn't an inheritance issue. If you set a break point in viewDidLoad of BaseViewController it does indeed execute that code. I think your issue is with how to change the tint color. Customizing the navigation bar in iOS is such a pain that I usually stray away from using navigation controller and end up just creating my own top bar view I can fully customize.

在我看来,这不是一个继承问题。如果你在viewDidLoad设置一个断点,它确实会执行那些代码。我想你的问题是如何改变颜色。在iOS中定制导航栏是一件非常痛苦的事情,我通常会偏离使用导航控制器,最终只创建我可以完全定制的自己的top bar视图。

If you are trying to change the tint color of both left and right bar items for your entire app put this code in your AppDelegate file inside the application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) function:

如果你想要改变整个应用程序左右栏的颜色,请将这段代码放到应用程序的AppDelegate文件中(应用程序:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)

UINavigationBar.appearance().tintColor = UIColor.greenColor()

Here's a blog post I created that shows how to change the other colors of UINavigationBar: Navigation Bar Colors.

下面是我创建的一个博客文章,展示了如何更改UINavigationBar的其他颜色:导航栏颜色。

#1


1  

From what I see, this isn't an inheritance issue. If you set a break point in viewDidLoad of BaseViewController it does indeed execute that code. I think your issue is with how to change the tint color. Customizing the navigation bar in iOS is such a pain that I usually stray away from using navigation controller and end up just creating my own top bar view I can fully customize.

在我看来,这不是一个继承问题。如果你在viewDidLoad设置一个断点,它确实会执行那些代码。我想你的问题是如何改变颜色。在iOS中定制导航栏是一件非常痛苦的事情,我通常会偏离使用导航控制器,最终只创建我可以完全定制的自己的top bar视图。

If you are trying to change the tint color of both left and right bar items for your entire app put this code in your AppDelegate file inside the application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) function:

如果你想要改变整个应用程序左右栏的颜色,请将这段代码放到应用程序的AppDelegate文件中(应用程序:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)

UINavigationBar.appearance().tintColor = UIColor.greenColor()

Here's a blog post I created that shows how to change the other colors of UINavigationBar: Navigation Bar Colors.

下面是我创建的一个博客文章,展示了如何更改UINavigationBar的其他颜色:导航栏颜色。