Does iOS allow developer define a private IBOutlet. For example, there are several buttons in a viewController, and I want to do something with these buttons both in Interface builder and code. However I do not want other class access these buttons. Can I define some "private" IBOutlets for this buttons
example code:
@interface myViewController : UIViewController<
{
@private:
UIButton *o_Button1;
UIButton *o_Button2;
}
//Can I have these outlets as private???
@property (nonatomic, retain) IBOutlet UIButton *Button1;
@property (nonatomic, retain) IBOutlet UIButton *Button2;
@end
===============================================================
Just get one solution. Wish it will help you.
Combine Abizern and JustSid ideas together, I have a solution like this.
in .h file
@interface myViewController : UIViewController
{
@private
IBOutlet UIButton *Button1;
IBOutlet UIButton *Button2;
}
@end
and in .m file
@interface MyViewController ()
@property (nonatomic, retain) UIButton *Button1;
@property (nonatomic, retain) UIButton *Button2;
@end
...
@synthesize Button1, Button2;
Thanks for help from Abizern and JustSid
Add the properties in a category at the top of the .m file:
@interface MyViewController ()
@property (nonatomic, retain) IBOutlet UIButton *Button1;
@property (nonatomic, retain) IBOutlet UIButton *Button2;
@end
In fact, this is how you can set up a property as readonly in the .h file and redeclare it as a readwrite property in the .m file - so you can have private setters.
@interface myViewController : UIViewController
{
@private
IBOutlet UIButton *o_Button1;
IBOutlet UIButton *o_Button2;
}
@end
This code allows you to have the outlet without a property that others might access.
The accepted answer above has the problem that IB won't be able to see the outlets.
The approach I use is to create a file called MyViewController-Protected.h and place the category with the private IBOutlets there. In your MyViewController.m you include the -Protected.h instead of the regular one.
The protected file could look like this:
// MyViewController-Protected.h
// Protected extensions to MyViewController
#import "MyViewController.h"
@interface MyViewController ()
@property (nonatomic, retain) IBOutlet UIButton *Button1;
@property (nonatomic, retain) IBOutlet UIButton *Button2;
@end
IBOutlets defined this way are only visible to classes including the Protected header file. This is only the class itself usually.
Once the category is in a protected header file, Interface Builder will be able to find the outlets. (For XCode3, you have to drag your -Protected.h file to IB, in Xcode4 it will work out of the box).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With