How do I create reusable iOS controls?
As a new iOS developer I am trying to determine the best approach to creating reusable controls in iOS4+. For example if I wanted to create a customized UIImageView that could be used in multiple other views what is the b开发者_如何学Pythonest approach.
I have created the control including the following files:
- MyImageViewController.h
- MyImageViewController.m
- MyImageViewController.nib
Assume that I want a control that utilizes a .nib file.
How would I do the following:
- Create an instance of the custom control in another view, programmatically and via IB?
- How do I structure the custom control so it can expose "events", e.g., ontouch?
- How do I structure the custom control so I can call actions/methods, e.g., loadImage()?
Any insights or reference appreciated. Thanks
You probably wouldn't use a nib/xib to create a reusable view 'control'. iOS is very different than what you may be used to from, say Java or .NET. For some kinds of views, you'll create a xib and controller/class which backs it - UITableViewCell is an example of this.
I wish I could give you a step by step guide how to achieve what you're trying to do, but I can't because I don't have enough info on what you are actually trying to do. There are several great guides on the apple developer site to get you acquainted with the View/Controller/Touch event interactions, but the site is not responding right now (I assume because of the release of the WWDC videos). Speaking of, watching videos from WWDC 2010 would be another great way to learn some of what you're trying to achieve, especially the UIKit sessions.
You aren't going to create 'controls' as you're probably used to them, but custom UIView-descendant objects and custom UIViewController-descendant objects. In my projects, I have found very very few scenarios where creating a reusable UIView was of any benefit (other than UITableViewCells). The vast majority of reusable code in an iOS project is ViewControllers. You'll expose events through the use of delegation - your controller will define a delegate which another object will implement, and receive the method calls from your controller to handle whatever was delegated. Again, the Apple guides and WWDC videos explain this very well.
Here's an example of a reusable controller and it's usage:
MyEntityViewController.m
- (id)initWithEntity:(EntityFoo *)theEntity inEditMode:(BOOL)inEditMode {
self = [super initWithNibNamed:@"EntityFooView" bundle:nil];
if(self) {
//retain entity somewhere
//dont setup view in here!
}
return self;
}
- (void)viewDidLoad {
//setup view in here. This ensures compatibility with standalone operation
}
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
EntityViewController *evc = [[EntityViewController alloc] initWithEntity:self.someEntity inEditMode:NO];
[self.view addSubview:evc.view];
//You MUST call this method, the framework will not call it on sub-controllers
[evc viewDidLoad];
self.entityViewController = evc;
[evc release];
}
This is not a complete implementation, as your experience and framework grows, you'll find a pattern of notifying (hint hint) sub-viewcontrollers of events in the main view controller. I strongly encourage you to look at the WWDC 2011 videos if you have access to them, as there is something coming in the next OS which has bearing on this subject matter.
Here's an article that describes how to create a reusable control with a nib. The section called "A reusable subview" sounds like what you want.
Contrary to what @RyanR said, it's perfectly fine to create a reusable control in iOS. It's not a pattern I've seen a lot, but you can totally do it.
This is my version for storyboards.
Add view controller id:
in .h file create a property to keep the controller. Otherwise it will be removed from the memory:
@property (nonatomic, strong) TestViewController *timetableViewController;
in .m file:
-(void)viewDidLoad{
[super viewDidLoad];
TestViewController *centerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"VIEW_CONTROLLER_ID"];
[self.view addSubview:centerViewController.view];
[centerViewController viewDidLoad];
self.timetableViewController = centerViewController;
}
精彩评论