对基类中的插座使用相同的属性名称/引用和潜在的错误 [英] Using the same property name/reference for Outlets in a base class and potential bugs

查看:43
本文介绍了对基类中的插座使用相同的属性名称/引用和潜在的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为 UITabBarTopLevelViewController 的基类UIViewController:

So I have a base class UIViewController called UITabBarTopLevelViewController:

class UITabBarTopLevelViewController: UIViewController {

@IBOutlet weak var uiNavItemTitle: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()
    uiNavItemTitle.titleView = CoreUtility.LogoForUINavBarGet()
    // Do any additional setup after loading the view.
}

然后我有两个从我的基类继承的UIViewControllers,它们都看起来像这样,除了第二个叫做 MyViewController2 :

I then have two UIViewControllers that inherit from my base class and both look like this, except the second is called MyViewController2:

class MyViewController1: UITabBarTopLevelViewController {

//@IBOutlet weak var uiNavItemTitle: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()
    //uiNavItemTitle.titleView = CoreUtility.LogoForUINavBarGet()

}

我向每个子UIViewController超级视图添加一个导航栏对象,然后CTRL拖动并向每个UIViewController子类添加一个新的出口:

I add a Navigation Bar object to each child UIViewController super view and then I CTRL drag and add a new outlet to each UIViewController child class:

这是第二个CTRL拖动出口:

And here is the second CTRL drag outlet:

这些是不同的引用,但是我可以在我的子类中注释掉 @IBOutlet弱uiNavItemTitle:UINavigationItem!,并且只能在基类 UITabBarTopLevelViewController 中引用一次,对于 MyViewController1 MyViewController2 .

These are different references, but I can comment out the @IBOutlet weak var uiNavItemTitle: UINavigationItem! in my child classes and reference one time only in the base class UITabBarTopLevelViewController, for both MyViewController1 and MyViewController2.

您可以看到,当我将鼠标悬停在出口圆圈上时,在基类中,它会突出显示故事板上的两个UINavigationItem:

You can see, when I hover over the outlet circle, in my base class, it highlights both UINavigationItem in the Story Board:

我对此功能感到惊讶,它对我来说很好,因为我只需要为两个视图的徽标一次设置 uiNavItemTitle.titleView 即可.这就是我想要的,但是我可以在基类中一次引用多个插座引用,并且似乎没有任何魔术或崩溃的地方有些不可思议.

I am surprised this works, its nice for me that it works because I only need to set the uiNavItemTitle.titleView for my logo one time for both views. This is what I want but there seems to be a bit of magic that I can reference multiple outlet references one time in my base class and there is no bugs or crashes.

我目前没有内存泄漏或崩溃,并且我的应用程序运行正常,并且完全按照我的意愿进行.

I currently have no memory leaks or crashes and my app is working just fine and doing exactly as I desire.

会不会有任何错误?

有人可以解释它的工作原理吗?

这是否有效,对于有经验的Swift开发人员来说不足为奇?

推荐答案

这就是子类的确切工作方式.

That's how subclass exactly works.

您已通过

uiNavItemTitle.titleView = CoreUtility.LogoForUINavBarGet()

uiNavItemTitle.titleView = CoreUtility.LogoForUINavBarGet()

此外, MyViewController1 MyViewController2 从基类 UITabBarTopLevelViewController 继承.也就是说,这些子viewController都具有一个UINavigationItem,它们从其 UITabBarTopLevelViewController 继承.

Also, MyViewController1 and MyViewController2 inherit from the base class UITabBarTopLevelViewController. That's say these child viewControllers both have a UINavigationItem which inherit from their UITabBarTopLevelViewController.

另一方面,这不是bug,更像是关于设计模式的主题.您可以将所有 base 内容放入一个基类,从这些类继承,并在子类中实现特定的细节.

This is not a bug, on the other hand, more like a topic about design pattern though. You could place all the base stuff into a base class, inherit from those classes and implement the specific detail within the child class.

HTH.

这篇关于对基类中的插座使用相同的属性名称/引用和潜在的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆