本文介绍了我如何使用多态来允许方法接受多个类,但使用IBOutlets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受 UIView 对象的特定方法,我想将对象传递给它,它可以是两个类中的一个。所以说它接受代表动物的 UIView ,我希望能够传递 DogView 和<$可能是c $ c> CatView 或其他动物类型。

I have a specific method that accepts a UIView object, and I want to pass objects to it that can be of one of two classes. So say it accepts a UIView that represents an animal, I want to be able to pass a DogView and a CatView or other animal-type classes potentially.

在该方法中我想设置 nameLabel 查看,所有动物都有。我如何设置它以便我能够做到这一点?

Within that method I want to set the nameLabel view, which all animals have. How do I set it up so I'd be able to do this?

我的第一反应是拥有一个超类(例如 AnimalView )上有 nameLabel 变量,然后为每个新动物创建子类。但是,如果我希望 nameLabel 成为一个出口,它似乎不可设置,因为我无法在每个子类中使用变量将视图连接到IB中。

My first reaction was to have a super class (such as AnimalView) that has the nameLabel variable on it, and then subclass it for each new animal. However, if I want the nameLabel to be an outlet, it doesn't seem settable as I couldn't have the variable in every subclass to wire the view up to in IB.

然后我尝试了协议,但这不是多态的,我无法访问nameLabel通过一般超类的财产,我可以吗?与Objective-C不同,我不能要求 UIView< ProtocolName> 然后它会允许我要求它。

I then tried a Protocol, but that's not polymorphic and I wouldn't be able to access the nameLabel property through a generic superclass, could I? Unlike Objective-C I couldn't ask for a UIView <ProtocolName> and it would then allow me to ask for it.

我该怎么做?我只是希望能够传递不同类型的对象并使其与Interface Builder兼容。我是否应该以完全不同的方式接近它?

How should I be doing this? I just want to be able to pass different kind of objects and have it be compatible with Interface Builder. Should I be approaching it completely differently?

推荐答案

您可以连接标签的出口到不同的 viewControllers 与你的 SuperClass 从故事板如果你的不同 viewControlelrs 在storyboard reperset中由子类(从SuperClass派生)在storyboard中。

You can connect outlet of label to different viewControllers with your SuperClass from story board if your different viewControlelrs in storyboard reperset by Subclasses(derived from SuperClass) names in storyboard.

1)只需定义

 class SuperClass{
     @IBOutlet weak var label: UILabel! = nil

 }

SubClass1 repersent 在storyboard中查看controller1 派生自 SuperClass
SubClass2 在源自 SuperClass

SubClass1 repersent view controller1 in storyboard derived from SuperClassSubClass2 repersent another view controller2 in storyboard derived from SuperClass

2)比转到助理编辑并打开 SuperClass 一边和另一边查看controller1 并将 SuperClass 的插座连接到<$中的标签 c $ c>查看controller1 .Drag从 SuperClass 标签到<$ c中的storyBoard $ c>查看controller1

2)Than Go to Assistant Editor and open SuperClass one side and other side view controller1 and connect outlet from SuperClass to label in storyBoard in view controller1.Drag from SuperClass label to storyBoard in view controller1

3)现在再次打开 SuperClass 一边和另一边查看controller2 并将 SuperClass 中的插座连接到中的storyBoard中的标签查看controller2 .Drag from Su perClass 标签中的storyBoard查看controller2

3)Now again open SuperClass one side and other side view controller2 and connect outlet from SuperClass to label in storyBoard in view controller2.Drag from SuperClass label to storyBoard in view controller2

如果你点击 SuperClass outlet,你会看到两个标签连接到不同的viewControllers

If you click on SuperClass outlet than you will see two labels conneted to different viewControllers

这篇关于我如何使用多态来允许方法接受多个类,但使用IBOutlets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:22