Unable to connect IBOutlet from storyboard to UIView subclass
Asked Answered
L

8

31

I am able to right-click and drag from my custom UIView subclass file to the storyboard elements to connect them, but unable to do so the other way around. I believe this is an Xcode bug. Is there anything that I can do to fix this? This is not the first time that it happen already and its annoying.

I already tried cleaning and restarting Xcode several times with no luck.

This does not work This does not work (Right clicking from the storyboard to Swift file)

This works This works (Right clicking from swift file's IBAction to storyboard)

Btw, I am using Xcode 8.1 with Swift 3.0.

UPDATE: I'm not looking for an answer on how to connect IBOutlets/IBActions, because as I mentioned in question above (and also in the screenshots), I am already able to connect them and the app is running fine. The question is more on WHY Xcode wouldn't let me connect from Storyboard > Swift file, but letting me to do so from Swift file > Storyboard. I'm sorry if the question is misleading.

Legible answered 2/12, 2016 at 12:23 Comment(4)
yes you can do first select UILable Ctrl+ Dreg with Mouse connect to swift file and create IBOutlets and IBActionsSickly
try first writing the IBOutlet and then connecting it to appropriate view.Myrlmyrle
@NikhilManapure Yeah that worked. But doing so the other way round does not. And what's weird is that it's working fine on my other ViewControllers, just this specific UIView that I'm unable to do so.Legible
Mostly this happens due to XCode bug, as XCode is getting too many updates and also sometimes due to XCode not able to identify class. This has happened to me too manytimes.Myrlmyrle
L
9

After trying some of the given solution what finally worked is by restarting the my macbook. It seems like a bug on XCode's side. Oh well.

Legible answered 2/12, 2016 at 13:1 Comment(1)
In my case, just quit and reopen XCode fixed it.Peltast
S
25

We see this issue sometimes too since Xcode 8, and assume this is a Xcode bug.

As a workaround:

  • Write the outlet in code @IBOutlet weak var myUiElement: UIView!

  • Ctrl + Drag from your outlet to the UI element in the Storyboard

Serin answered 2/12, 2016 at 13:24 Comment(1)
I'm having the same problem, but when I do what you've described, the vars are nil when I run the app. xcode shows the connection, but I don't think it's really happening since the vars are nil. I'm using xcode 9 and I know that it's buggy as hell, but maybe IBOutlets aren't supported in UIView subclasses.Stearne
P
25

Its basic, but easy to overlook. Make sure the view controller you are dragging the outlet to is labeled as the custom class you created.

enter image description here

Premedical answered 16/1, 2018 at 5:56 Comment(1)
How is this not toggled by default in Xcode after creating a xib? Thanks for the answer. Saved me some headaches...Buskirk
L
9

After trying some of the given solution what finally worked is by restarting the my macbook. It seems like a bug on XCode's side. Oh well.

Legible answered 2/12, 2016 at 13:1 Comment(1)
In my case, just quit and reopen XCode fixed it.Peltast
M
6

Create an outlet like this in Objective-C

@property (nonatomic, weak) IBOutlet UIView *view;

or as in swift 3

@IBOutlet weak var instruction: UILabel?

and then connect it as given in this image.

enter image description here

Sometimes the normal way of connecting action and outlet doesn't work due to subclassing. This way comes handy for such situations.

Myrlmyrle answered 2/12, 2016 at 12:53 Comment(0)
W
2

Things to check if IBOutlet does not connect:

  1. Is the class defined in storyboard?

    Select the UIViewController in storyboard, then on the right side of the screen, select the identity inspector and assign the appropriate class.

  2. Are the type of outlet you defined match the one on storyboard? For example, we connect UIButton, then the outlet should look like that, write the code for the outlet, then connect to the view controller:

    @IBOutlet weak var theButton: UIButton!
    
Weltschmerz answered 2/12, 2016 at 12:44 Comment(0)
U
1

I've resolved my issue by following process.

  1. Connect your Storyboard view with any other ViewController.
  2. Now, reconnect your desired ViewController with storyboard.

Hope this will work for you!!

Unhandled answered 23/7, 2019 at 6:18 Comment(0)
S
0

In my case I had my class MyViewController share a file with another view controller and the file was actually named after that other viewController. Once I moved MyViewController to its own file it started working.
In case this wasn't clear here is a version as code:

/*  Version one (did not work): */

// OtherViewController.swift
class OtherViewController: UIViewController {
 // stuff
}
class MyViewController: UIViewController {
 // could not control-drag view here to create IBOutlet/IBAction
}

/*  Version two (works): */

// OtherViewController.swift
class OtherViewController: UIViewController {
 // stuff
}

// MyViewController.swift
class MyViewController: UIViewController {
 // yay! now I can control-drag a view here!

}
Stupe answered 17/8, 2021 at 9:16 Comment(0)
S
-3

Another way just create your button name manually in view controller like this -

@IBOutlet weak var button: UIButton!

then connect your button via drag to view controller as image shown [! enter image description here

PS:- please clean your derived data also.

Strange answered 2/12, 2016 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.