Unable to connect IBOutlet from storyboard to UIView subclass

前端 未结 9 766
旧巷少年郎
旧巷少年郎 2020-12-05 10:11

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

相关标签:
9条回答
  • 2020-12-05 10:24

    1: Assign the class to viewController.

    2: Clean your project with Shift-Alt-Cmd-K.

    3: First quit Xcode and clear the derived data. going to: ~/Library/Developer/Xcode/DerivedData

    0 讨论(0)
  • 2020-12-05 10:29

    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.

    0 讨论(0)
  • 2020-12-05 10:35

    press ctrl than drag to swift file and create outlet.

    0 讨论(0)
  • 2020-12-05 10:40

    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!
      
    0 讨论(0)
  • 2020-12-05 10:42

    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.

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

    0 讨论(0)
  • 2020-12-05 10:42

    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 [!

    PS:- please clean your derived data also.

    0 讨论(0)
提交回复
热议问题