[英]Using a custom UIView from multiple view controllers

I have created a custom UIView that I would like to use on multiple different view controllers in my iPhone application. Currently, I have copied the UIView that contains the controls from the nib file for the first view controller, pasted it into the other view controllers, and then wired up all of the actions to each view controller. This works fine, but is not the optimal way that I would like to use this custom view.


My custom view is reasonably simple, it consists of some UIButtons with labels, and tapping on these buttons fires actions that changes the contents of controls on my view controller's view.


What would be a strategy to consolidate the definition and usage of this UIView? Ideally, I would like to just reference this custom view from the nib of view controllers, and have my view controller respond to actions from this custom view.


EDIT: OK, based on J.Biard's suggestions, I have tried the following with not much luck.


I created another UIView based nib file with the contents (for now just some UIButton objects) of my reusable view and UIView subclass .m and .h files, and then set the nib File's Owner class to my newly created class name.


Then, I added most of the code from J.Biard (I changed the rect to 50,50,100,100, left out the setDelegate out for now, as I am just trying to get it working visually for now, and i found that [self.view addSubview:view] worked much better than [self addSubView:view]) to the end of the viewDidLoad method of the first view controller that is displayed when the app fires up.

然后,我添加了來自J.Biard的大部分代碼(我將rect更改為50,50,100,100,暫時省略了setDelegate,因為我現在只是想讓它在視覺上工作,我發現[自我。查看addSubview:view]比[self addSubView:view]更好地工作到應用程序啟動時顯示的第一個視圖控制器的viewDidLoad方法的末尾。

And what I get now is my main view with a black square in it. Have I missed an outlet somewhere, or is there some initialization needed in initWithFrame or drawRect in the UIView subclass?


2 个解决方案


If it is very simple I would recommend that you create a subclass of UIView in code and create instances of this class (or you can use Interface Builder to create the custom UIView that is then archived into the NIB file and restored later on also using code).

如果它非常簡單,我建議您在代碼中創建UIView的子類並創建此類的實例(或者您可以使用Interface Builder創建自定義UIView,然后將其歸檔到NIB文件中,並在以后使用代碼進行恢復)。

Using the code solution you could create instances of your custom UIView in your controller by calling something like:


#import "MyCustomView.h"

// let the superview decide how big it should be or set it as needed.
CGRect sizeRect = CGRectMake(0,0,0,0);

// create an instance of your view   
MyCustomView *view = [MyCustomView alloc] initWithFrame:sizeRect];

// set a custom delegate on the view or set callback methods using @selector()...
[view setDelegate:self];  // self = view controller

// add the view to the controller somewhere... (eg: update CGRect above as needed)
[self addSubView:view];

// don't forget to release the view somewhere ;-)

This example assumes that you create a delegate protocol that your View Controller can respond to or you can wire up events dynamically using @selector. If you don't want to create instances of the view in code you could add a "UIView" to your NIB file and set it's class type in the inspector window (command -> 4 -> class dropdown).

此示例假定您創建了View Controller可以響應的委托協議,或者您可以使用@selector動態連接事件。如果您不想在代碼中創建視圖實例,可以在NIB文件中添加“UIView”,並在檢查器窗口中設置它的類類型(命令 - > 4 - >類下拉列表)。

If you want to do everything in interface builder you can create your custom UIView and use something like "- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options" (see NSBundle) to load the NIB file dynamically. This presents it's own challenges though it is also feasible.

如果你想在界面構建器中做所有事情,你可以創建自定義UIView並使用類似“ - (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options”(參見NSBundle)動態加載NIB文件盡管它也是可行的,但它提出了它自己的挑戰。

The most involved option would be to create your own xcode custom UI library / plugin so that your custom control / view could be dragged into each NIB file from the Library window like any other control shipped by Apple.


Hope this clarifies or eliminates some options for re-using controls for you.




Create your MyCustomView class and nib file.


In the nib file, set Files Owner to MyCustomView - then design your view as normal, with a top level UIView. Create an IBOutlet in MyCustomView to link to your top level UIView in your nib file.

在nib文件中,將Files Owner設置為MyCustomView - 然后正常設計您的視圖,使用頂級UIView。在MyCustomView中創建一個IBOutlet,以鏈接到nib文件中的頂級UIView。

In MyCustomView add this method:


- (BOOL) loadMyNibFile {
if (![[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]) {
    return NO;
return YES;

In your view controllers you can use this custom view like so


- (void)viewDidLoad {

MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[customView loadMyNibFile];
[self.view addSubview:customView.view]; //customView.view is the IBOutlet you created
    [customView release];


You could also create a convenience class method on MyCustomView to do this if you liked.




粤ICP备14056181号  © 2014-2021 ITdaan.com