Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, iboutlet and custom controls

I may be doing something really stupid, but I don't seem to be able to use Interface Builder to connect IBOutlet variables to custom views, but only in Swift.

I've created a class called MyView, which extends from UIView. In my controller, I've got a MyView variable (declared as @IBOutlet var newView: MyView). I go into IB and drag a UIView onto the window and give it a class of MyView.

Whenever I've done similar in Objective C, I'm then able to click on the View Controller button at the top of the app window, select the variable and drag it down to the control to link the two together. When I try it in Swift, it refuses to recognise that the view is there.

If I change the class of the variable in the controller to UIView, it works fine. But not with my custom view.

Has anyone else got this problem? And is it a feature, or just my idiocy?

Code for Controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet var newView:MyView

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Code for view

import UIKit

class MyView: UIView {

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

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect)
    {
        // Drawing code
    }
    */

}
like image 528
hobart Avatar asked Jun 08 '14 09:06

hobart


2 Answers

I've had a similar problem, and I think it's partially a caching issue and partially just an Xcode6/Swift issue. The first step I found was required was to make sure that the view controller .swift file would be loaded in the Assistant Editor when choosing "automatic".

With Xcode finding that both the files are linked I could sometimes control-drag from the view/button/etc. from the IB to the .swift file, but often had to drag from the empty circle in the gutter of the @IBOutlet var newView:MyView line to the view I wanted it to match up to.

If you can't get the file to load in the Assistant Editor then I found that doing the following would often work:

  1. Remove the custom class from the IB view
  2. Clean the project (cmd + K)
  3. Close/reopen Xcode
  4. Possibly clean again?
  5. Add the custom class back to the view
  6. Hope it works :)

If that seems to get you half way/nowhere add a comment and I'll see if it triggers anything else I did

like image 62
Joseph Duffy Avatar answered Sep 20 '22 06:09

Joseph Duffy


In my case import UIKit was missing, after adding this line I could create an IBOutlet from Storyboard again.

like image 36
gpichler Avatar answered Sep 21 '22 06:09

gpichler