Swift, iboutlet and custom controls Swift, iboutlet and custom controls xcode xcode

Swift, iboutlet and custom controls


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


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


I've had a similar problem to the one described in this thread. Maybe you found a solution maybe not but anybody who encounters this in the future. I've found the key is to use the "required init" function as follows:

required init(coder aDecoder: NSCoder) {    print("DrawerView: required init")    super.init(coder: aDecoder)!    screenSize = UIScreen.mainScreen().bounds    screenWidth = screenSize.width    screenHeight = screenSize.height    self.userInteractionEnabled = true    addCustomGestureRecognizer()}

This is the complete class of my custom view:

import UIKitimport Foundation

class DrawerView: UIView {

var screenSize: CGRect!var screenWidth: CGFloat!var screenHeight: CGFloat!var drawerState: Int = 0override init (frame : CGRect) {    print("DrawerView: main init")    super.init(frame : frame)}override func layoutSubviews() {    print("DrawerView: layoutSubviews")    super.layoutSubviews()}convenience init () {    self.init(frame:CGRect.zero)}required init(coder aDecoder: NSCoder) {    print("DrawerView: required init")    super.init(coder: aDecoder)!    screenSize = UIScreen.mainScreen().bounds    screenWidth = screenSize.width    screenHeight = screenSize.height    self.userInteractionEnabled = true    addCustomGestureRecognizer()}func addCustomGestureRecognizer (){    print("DrawerView: addCustomGestureRecognizer")    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:)))    swipeDown.direction = UISwipeGestureRecognizerDirection.Down    self.addGestureRecognizer(swipeDown)    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:)))    swipeUp.direction = UISwipeGestureRecognizerDirection.Up    self.addGestureRecognizer(swipeUp)    print("DrawerView self: \(self)")}func minimizeDrawer(){    UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {        //            let height = self.bookButton.frame.size.height        //            let newPosY = (self.screenHeight-64)*0.89        //            print("newPosY: \(newPosY)")        self.setY(self.screenHeight*0.86)        }, completion: { finished in            self.drawerState = 0            for view in self.subviews {                if let _ = view as? UIButton {                    let currentButton = view as! UIButton                    currentButton.highlighted = false                } else if let _ = view as? UILabel {                    let currentButton = view as! UILabel                    if self.tag == 99 {                        currentButton.text = "hisotry"                    } else if self.tag == 999 {                        currentButton.text = "results"                    }                }            }    })}func handleDrawerSwipeGesture(gesture: UIGestureRecognizer) {    print("handleDrawerSwipeGesture: \(self.drawerState)")    if let swipeGesture = gesture as? UISwipeGestureRecognizer {        switch self.drawerState{        case 0:            if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down {                // nothing to be done, mini and swiping down                print("mini: !")            } else {                // mini and swiping up, should go to underneath city box                UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: {                    let toYPos:CGFloat = 128 + 64 + 8                    self.setY(toYPos)                    }, completion: { finished in                        self.drawerState = 1                        for view in self.subviews {                            if let _ = view as? UIButton {                                let currentButton = view as! UIButton                                currentButton.highlighted = true                            } else if let _ = view as? UILabel {                                let currentLabel = view as! UILabel                                currentLabel.text = "close"                            }                        }                })            }            break;        case 1:            if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down {                // open and swiping down                self.minimizeDrawer()            } else {                // open and swiping up, nothing to be done            }            break;        default:            break;        }    }}

}

Hope this helps...