In my project I have a view (defined in a xib) that is a reusable component and will be places in many other views, I have my view controller class designed to handle different modes for this view.
My apps have 3 tabs basically use this same view, by initialize with different parameters they work pretty nice. I load this view from the Interface Builder (by specify the NIB name and class name), but I will have to pass different parameters so my view controller will know which mode it is.
I am wondering can开发者_StackOverflow中文版 I define some 'parameter' from Interface Builder, so my view controller class can simply look up and determinate itself. So I don't need to write any additional code to reuse this nib in many different places?
Your other option then would be to use the tag parameter on the view controller's view - if you're not already using it for something else.
Your viewController would then read it's main view's tag and change it's behaviour accordingly.
myViewControllersView.tag = i;
I'd look into creating your own custom init methods in your view controllers and use these to initialise your view controllers.
myViewController = [[[myUIViewController alloc] init] initWithParamOne:paramOne andParamTwo:paramTwo];
- (id) initWithParamOne:(NSString *)paramOne andParamTwo:(NSString *)paramTwo {
self = [super initWithNibName:@"myNib" bundle:nil];
if (self) {
// code here
}
}
I end up use the "title" to pass a json string and it works for me pretty well, I am not sure if there is a better way to do so.
精彩评论