How to access IBOutlets declared in superclass?

前端 未结 10 1439
执笔经年
执笔经年 2020-12-30 03:24

I\'m currently refactoring a couple of view controllers that share a few IBOutlets and IBAction methods. I moved the outlet declarations and the

相关标签:
10条回答
  • 2020-12-30 03:49

    I had the same problem, and it turns out it was because in the superclass I had the IBOutlets declared as "_Nullable". Example:

    @property (nonatomic, strong)   IBOutlet UITableView *_Nullable mySuperTableView;
    

    When I removed the _Nullable, suddenly the IBOutlets reappeared in IB and all was good again. (I had only set them to _Nullable because Xcode was complaining "pointer is missing a nullability type specifier"... (don't know why). )

    0 讨论(0)
  • 2020-12-30 03:54

    The simplest way: create interface and implementation files for your subclass(es)!
    Perfect example: Juggleware's awesome ShadowButton Subclass of UIButton.

    Make sure to create the .h & .m files in your project. NOTE: There is no need to #import the header files at all since this is simply a class instance of UIButton.

    In Interface Builder:

    1. Select the element you which to connect.
    2. Go to Utilities -> Identity Inspector
    3. Change the Class to your subclass (or superclass). NOTE: You might have to type in your subclass name and hit ENTER.

    You're done!

    Even if you have declared a basic class (UIButton) as IBOutlet in your header file like so...

    // YourViewController.h
    
    @interface YourViewController : UIViewController {
    
    IBOutlet UIButton *mybutton;
    }
    

    ...the class you've set in Interface Builder (ShadowButton) will overwrite it since it's in the view layer.

    The best part about this approach is that your code doesn't have any messy dependency issues.

    0 讨论(0)
  • 2020-12-30 03:56

    I didn't realize it was even possible to connect to superclasses in interface builder until about an hour ago. Since this was the only question I could find regarding how to do this, I'll add my answer, even though this question is old. My answer is with regard to Xcode 4, not Xcode 3.

    As far as I can tell, you can't connect to outlets in a superclass using the assistant editor, but you can do it by clicking on "File's Owner" in IB. That should show all the outlets in Utilities->Connections Inspector. You can then Ctrl+Click on the outlet in the inspector (click on the '+' sign), and drag it over to your view in IB.

    0 讨论(0)
  • 2020-12-30 03:59

    The solution for the problem with the IBOutlet .. is to change the class type to the Base Class in the identity inspector

    enter image description here

    connect using Control + drag and drop and

    enter image description here

    change it back to the child class

    enter image description here

    This works for me

    BTW: i used Xcode 6

    0 讨论(0)
  • 2020-12-30 03:59

    I'm doing this in XCode 3.2.6. I started with outlets connected to a class, and then made a subclass with additional outlets. When I changed the File's Owner class to the subclass, IB showed the superclass outlets as greyed out. I switched File's Owner to the superclass, then back to the subclass and now all outlets are showing not greyed out.

    0 讨论(0)
  • 2020-12-30 03:59

    On the project I am currently working, we have a BaseViewController with a UIScrollView as IBOutlet and handles keyboard appearance/disappearance events and slides the content accordingly. At first, I could not connect to that IBOutlet, than solved the problem like this, which is similar to Sosily's answer:

    • BaseViewController has an IBOutlet, called contentScrollView. I can see 5 previously connected outlets, which are UIScrollViews on other UIViewControllers, created by people who previously worked on the project

    • I tried to connect my UIScrollView as the contentScrollView. Although my UIViewController is a subclass of BaseViewController, I cannot connect it.

    • I tried to connect already connected UIScrollViews as the contentScrollView. Although all UIViewControllers are subclasses of BaseViewController, I cannot connect them again, as well. So, I started to look for a trick.

    • I have created the same contentScrollView IBOutlet on my own UIViewController, connected the scrollView to my own contentScrollView outlet and removed the one that I have just created.

    • Now the scrollView is connected as contentScrollView to File's Owner, but the only contentScrollView belongs to the BaseViewController. Tested and verified that keyboard events are handled correctly.

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