How to access IBOutlets declared in superclass?

前端 未结 10 1503
执笔经年
执笔经年 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题