简体   繁体   English

无法弄清楚如何使用Swift将UIView类连接到IB中的View

[英]Can't figure how to to hook up a UIView class to a View in IB using Swift

** Updated ** I'm working on an idea in Swift and can't seem to figure out how to get a view hooked up to a view class. **更新了**我正在Swift中研究一个想法,似乎无法弄清楚如何将视图连接到视图类。

I have a UIViewController in IB and a UIViewController class that is associated to that UIViewController in IB. 我在IB中有一个UIViewController,在IB中有一个与该UIViewController关联的UIViewController类。 Inside the UIViewController and drug in UIView in IB. 在UIViewController内部和IB中的UIView中使用毒品。 I want to give that UIView it's own class. 我想给UIView自己的类。 I have a Text Field inside of the View and I would like the UIView class to handle all of the setup code for that text field. 我在视图内有一个文本字段,我希望UIView类可以处理该文本字段的所有设置代码。 I able to create the UIView class, but and I was also able to set it as the bast class for the View in IB. 我可以创建UIView类,但是也可以将其设置为IB中的View的最高级。 What I'm not 100% sure is how to Init this class so that I can create vars in the UIView class and control drag them to my Text Field in IB, 我不是100%不确定如何初始化此类,以便可以在UIView类中创建var,然后控制将其拖到IB中的“文本字段”中,

* Updated * This is my code so far.. it seems to be at least doing something but I'm getting the fetal error print message *更新*到目前为止,这是我的代码..似乎至少在做点什么,但是我收到了胎儿错误打印消息

import UIKit

class AddPlayerView: UIView
    @IBOutlet weak var inputPlayerOne: UITextField!
    @IBOutlet weak var addPlayerOne: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code

        println("init add player")

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

This I get this printed out 我把这个打印出来

fatal error: init(coder:) has not been implemented: 致命错误:尚未实现init(coder :):

I just don't get your question. 我只是不明白你的问题。 Initializing the view withing IB will fire the init(coder aDecoder: NSCoder) rather then init(frame: CGRect) and this is standard. 使用IB初始化视图将触发init(编码器aDecoder:NSCoder),而不是init(帧:CGRect),这是标准的。 Now if your concern is to access some variables not intialized then just override the func awakeFromNib(). 现在,如果您要访问的是一些未初始化的变量,则只需重写func awakeFromNib()函数即可。 And implement the required initializer just with the super 并使用super实现所需的初始化程序

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override awakeFromNib(){
    // Initialization code
}

Does this make sense? 这有意义吗?

You can create an Outlet following these steps (tested with Xcode6-Beta6 + Swift): 您可以按照以下步骤创建一个插座(已通过Xcode6-Beta6 + Swift测试):

  1. In Xcode activate the Assistant editor (you should have Storyboard on the left side of the screen and the Swift code on the right) 在Xcode中,激活Assistant编辑器 (您应该在屏幕左侧显示Storyboard,在右侧显示Swift代码)
  2. On the left select the a View (now on the right you should have the Swift Controller related to that view) 在左侧选择一个视图(现在在右侧,您应该具有与该视图相关的Swift控制器)
  3. On the left perform a right click on a UI element (eg a UIButton in your view) 在左侧,右键单击UI元素(例如,视图中的UIButton)
  4. A dark transparent panel will appear over the UI element UI元素上方将出现一个黑色透明面板
  5. Left click on the "circle/plus" button near the item New Referencing Outlet 左键单击 新建参考插座 附近的“圆圈/加号”按钮
  6. Keep holding the left click and drag a line to the Swift Controller (eg where you would declare an instance property). 按住鼠标左键并将一条线拖到Swift Controller(例如,在其中声明实例属性的位置)。
  7. Release the left click 释放左键
  8. Xcode will ask you to give a name to the Outlet, type something like "button" Xcode会要求您为插座命名,输入类似“按钮”的名称
  9. Click Connect 点击连接

Done. 完成。 Now in your swift class you have a reference to your UIButton. 现在,在您的swift类中,您可以引用UIButton。

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    ...

Hope this helps. 希望这可以帮助。

Storyboard uses it's own initializer: init(coder:) to initialize its xibs. 故事板使用它自己的初始化程序: init(coder:)初始化其xib。 This also means that if you override one initializer, you must override the storyboard's required initializer as well (the coder one previously mentioned). 这也意味着,如果您覆盖一个初始化程序,则也必须覆盖故事板required初始化程序(前面提到的coder )。 It appears as if you did this, however for some reason you have put fatalError("init(coder:) has not been implemented") as your code in required init(coder aDecoder: NSCoder) . 看起来好像您这样做了,但是由于某种原因,您已经将fatalError("init(coder:) has not been implemented") as your code required init(coder aDecoder: NSCoder)
This means that every time your views are initialized, that method is being called, and of course its going to throw a fatal error and crash your app. 这意味着每次您的视图初始化时,都会调用该方法,当然,该方法将引发致命错误并使应用程序崩溃。
We must replace that code. 我们必须替换该代码。 Be sure to call super.init(coder: aDecoder) at the beginning of the method, and then place the rest of your initializer code afterwards. 确保在方法的开头调用super.init(coder: aDecoder) ,然后将其余的初始化代码放在super.init(coder: aDecoder)
You might also find this Stack Overflow answer useful: Fatal error: use of unimplemented initializer 'init(coder:)' for class 您可能还会发现此堆栈溢出答案很有用: 致命错误:对类使用未实现的初始化程序'init(coder :)'
Please let me know if you have any more problems. 如果您还有其他问题,请告诉我。 Good luck! 祝好运!

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

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