0

** 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.

I have a UIViewController in IB and a UIViewController class that is associated to that UIViewController in IB. Inside the UIViewController and drug in UIView in IB. I want to give that UIView it's own class. 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. 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. 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,

* 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:

icekomo
  • 8,336
  • 7
  • 27
  • 53
  • Have you thought of using the *Tap Gesture Recognizer* for dismissing the keyboard? With *resignFirstResponder*? – chuckus Aug 30 '14 at 01:48
  • You were never able to "just drag" from something in a nib/storyboard to a UIControl class. Swift is irrelevant here. What kind of connection would you want to make? There's nothing that makes sense, so there's nothing obvious for Xcode to do. It is far from obvious what you're expecting or trying to do. – matt Aug 30 '14 at 03:36
  • Hey Matt, Maybe I'm not explaining myself properly. In IB I created a view and inside the view I placed a text field. I think created a UIControl class. After that from the custom class drop down I would select the UIControl class that I just created. So that my UIControl class is now linked to my View in IB. I would then create a IBOutlet and connect that to something inside the view in IB. I was doing this to keep all of the text input code apart from the viewController code. I guess what I am not understanding is how to use an UIControl class on it's own. – icekomo Aug 30 '14 at 04:36

3 Answers3

1

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. Now if your concern is to access some variables not intialized then just override the func awakeFromNib(). And implement the required initializer just with the super

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

override awakeFromNib(){
    // Initialization code
}

Does this make sense?

DennyLou
  • 303
  • 2
  • 4
0

You can create an Outlet following these steps (tested with 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)
  2. On the left select the a View (now on the right you should have the Swift Controller related to that view)
  3. On the left perform a right click on a UI element (e.g. a UIButton in your view)
  4. A dark transparent panel will appear over the UI element
  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 (e.g. where you would declare an instance property).
  7. Release the left click
  8. Xcode will ask you to give a name to the Outlet, type something like "button"
  9. Click Connect

Done. Now in your swift class you have a reference to your UIButton.

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    ...

Hope this helps.

Luca Angeletti
  • 53,311
  • 9
  • 104
  • 138
0

Storyboard uses it's own initializer: init(coder:) to initialize its xibs. This also means that if you override one initializer, you must override the storyboard's required initializer as well (the coder one previously mentioned). 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).
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.
You might also find this Stack Overflow answer useful: Fatal error: use of unimplemented initializer 'init(coder:)' for class
Please let me know if you have any more problems. Good luck!

Community
  • 1
  • 1
dcgoss
  • 2,041
  • 2
  • 17
  • 29