使用來自多個視圖控制器的自定義UIView

[英]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.

我創建了一個自定義UIView,我想在我的iPhone應用程序中的多個不同視圖控制器上使用它。目前,我已經復制了包含第一個視圖控制器的nib文件中的控件的UIView,將其粘貼到其他視圖控制器中,然后將所有操作連接到每個視圖控制器。這工作正常,但不是我想使用此自定義視圖的最佳方式。

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.

我的自定義視圖相當簡單,它包含一些帶標簽的UIButtons,點擊這些按鈕會觸發更改控制器視圖控件內容的操作。

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.

什么是鞏固這個UIView的定義和用法的策略?理想情況下,我想從視圖控制器的nib引用此自定義視圖,並讓我的視圖控制器響應此自定義視圖中的操作。

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

編輯:好的,根據J.Biard的建議,我嘗試了以下運氣並沒有太多運氣。

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.

我創建了另一個基於UIView的nib文件,其中包含我的可重用視圖和UIView子類.m和.h文件的內容(現在只是一些UIButton對象),然后將nib文件的Owner類設置為我新創建的類名。

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?

我現在得到的是我的主視圖,其中有一個黑色方塊。我是否錯過了某個地方的插座,或者是否在UIView子類中的initWithFrame或drawRect中需要初始化?

2 个解决方案

#1


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:

使用代碼解決方案,您可以通過調用以下內容來在控制器中創建自定義UIView的實例:

#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.

最常用的選項是創建自己的xcode自定義UI庫/插件,以便您可以將自定義控件/視圖從“庫”窗口拖動到每個NIB文件中,就像Apple提供的任何其他控件一樣。

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

希望這能澄清或消除一些為您重復使用控件的選項。

Cheers-

#2


Create your MyCustomView class and nib file.

創建MyCustomView類和nib文件。

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:

在MyCustomView中添加此方法:

- (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.

如果您願意,還可以在MyCustomView上創建一個便捷類方法來執行此操作。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/07/73022d2ec33d52ad3d319426cb60f07.html



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