开发者

Reuse of code-based UIViews compared with XIBs

Say that I have a UIView which I would like to re-use in multiple view controllers, given that it is a fairly generic UIView object with high re-usability value (like a UITextField or UISegmentedControl object). From my understanding it would be much easier to reuse this UIView if I wrote the UIView in code, rather than making a XIB using Interface Builder. I have reached this conclusion because if the UIView is written in code then I can simply initialize a new instance of that UIView in any view controller:

MyGreatReusableView *greatReusableView = [[MyGreatReusableView alloc] initWithFrame:CGRectMake(...)];

...and then directly access the properties and attributes of that UIView, just like with the stock UIView controls such as UITextField with it's .text, .textColor, etc. properties.

However, if I create the UIView as a XIB then it must be tied (through either the File's Owner or an IBOutlet on the View object in the XIB) to a specific view controller, and therefore can only be used in that view controller. Furthermore, I can only access the properties of controls on the XIB using the IBOutlets which are connected to that view controller.

I am thinking that I have completely misunderstood something with regards to XIB files, as this does seem to be quite a limitation! If someone could provide clarification on whether it is possible to re-use XIB files in multiple view controllers and, if so, provide an example of a code-based solution vs. a XIB-based solution I would be very much appreciative.

Thanks in advance开发者_JAVA技巧 for any assistance!


An alternative solution, without the need of setting the IBOutlets of the reusable view would be by assigning the subviews you want to use with a specific tag. Then you could obtain the views with the tag.

For example when you want to create a view with a label and a text field. The process for doing this would be:

  1. Create an empty xib
  2. Setup a view with a label and a text field as a subview.
  3. Assign the label and the text field different tags (let's say 10 and 11)

  4. Create a header file for this xib and define macros for the tags
    #define kReusableViewNibName @"ReusableViewNibName"
    #define kLabelTag 10
    #define kTextFieldTag 11

  5. You can now load the xib in any view controller you want to:


// Import header file with previous defines

NSArray *rootViewsFromXib = [[NSBundle mainBundle] loadNibNamed:@"NameOfXib"
                                                          owner:nil
                                                        options:nil];

// There is only one root view in the xib,
// the view that contains the label and text field
UIView *loadedView = [rootViewsFromXib objectAtIndex:0];

// You can now use the views from within the xib
// by using the tags to obtain the views.
UILabel *label = (UILabel *)[loadedView viewWithTag:kLabelTag];
UITextField *textField = (UITextField *)[loadedView viewWithTag:kTextFieldTag];


It is indeed possible to use the same view in multiple view controllers. You were on the right track. The key is that you don't have to use the view controller to which the view is tied. Here is what I do for re-usable table cells (that contain things like sliders)

  1. Create a new XIB file
  2. Create a new view (in my case this was a table cell)
  3. Set the UIViewController.view to be the view (again, table cell)
  4. I hook it up with this code (currently used in production):
    
    @implementation TableCellFactory
    +(UITableViewCell*) createCellInstanceFromXIBName:(NSString*)name {
            UIViewController* tmp = [[UIViewController alloc] initWithNibName:name bundle:nil];
            UITableViewCell* cell = (UITableViewCell*)[tmp view];
            DebugAssert([[cell class] isSubclassOfClass:[UITableViewCell class]], @"XIB view cell is not a subclass of UITableViewCell; xib name:%@",name);
            DebugAssert(cell != nil,@"cell is nil -- Xib name:%@",name);
            [cell retain];
            [tmp release];
            return cell;
    }
    @end
    
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜