简体   繁体   English

继承视图控制器中的出口和动作

[英]Inherit outlets and actions in view controller

i've create TabBar App with 5 tabs, all 5 views are the same and each view include buttons to control one set of equipment, switch input to output on receiver and control volume, so 5 views correspond to 5 rooms. 我已经创建了具有5个选项卡的TabBar App,所有5个视图都是相同的,并且每个视图都包含用于控制一组设备,将输入切换为接收器上的输出并控制音量的按钮,因此5个视图对应于5个房间。 Now i have 5 different UIViewCOntrollers with nerly the same code, inherited from UIViewController . 现在我有5个不同的UIViewCOntrollers ,它们具有相同的代码,继承自UIViewController

But i think it should be one RoomViewController, inhereted from UIViewController , and RoomViewCOntroller shold be used for each room, something like this 但是我认为应该是一个UIViewController继承的RoomViewController,每个房间都应使用RoomViewCOntroller shold,类似这样

class RoomViewController: UIViewController {

    @IBOutlet weak var videoSat: UIButton!

   func lockAudioLabel(sender: UIButton) {
      if sender.isSelected {
         return
      } 
      videoSat.isSelected = false
   }

  @IBAction func videoSatSelect(_ sender: UIButton) {

    //send command to equipment
    //make some actions
    lockVideoLabel(sender: sender)
  }
}

And Room1ViewController is 而Room1ViewController是

class Room1ViewController: RoomViewController {
}

All rooms have scene in storyboard 所有房间的故事板上都有场景

So the question is there any way to use one set of iboulets and ibactions in RoomViewController class and inherit them for example in Room1ViewContoller? 所以问题是,有没有办法在RoomViewController类中使用一组iboulet和ibaction并继承它们,例如在Room1ViewContoller中? And how to access them? 以及如何访问它们?

So the question is there any way to use one set of iboulets and ibactions in RoomViewController class and inherit them for example in Room1ViewContoller? 所以问题是,有没有办法在RoomViewController类中使用一组iboulet和ibaction并继承它们,例如在Room1ViewContoller中?

The @IBOutlet properties and @IBAction methods you declare in RoomViewController are inherited by subclasses such as Room1ViewController. 您在RoomViewController 声明的@IBOutlet属性和@IBAction方法由子类(例如Room1ViewController)继承。

But what is not inherited is the design in the nib. 但是笔尖的设计并没有被继承。 When you instantiate a subclass like Room1ViewController, it loads its own view, not some other view controller's view. 当实例化诸如Room1ViewController之类的子类时,它会加载其自己的视图,而不是其他视图控制器的视图。 This loading takes place on a per-instance basis. 此加载是按实例进行的。 Therefore, you still need to hook up the interface objects in the nib to the properties and methods in the class declaration. 因此,您仍然需要将笔尖中的接口对象连接到类声明中的属性和方法。 In other words, you still need to make outlets and actions in the nib, because this is a different view. 换句话说,您仍然需要在笔尖中进行输出和操作,因为这是不同的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM