使用来自多个视图控制器的自定义UIView [英] Using a custom UIView from multiple view controllers

查看:92
本文介绍了使用来自多个视图控制器的自定义UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义的UIView,我想在我的iPhone应用程序中的多个不同的视图控制器。目前,我已经从第一个视图控制器的nib文件复制了包含控件的UIView,将其粘贴到其他视图控制器,然后将所有操作连接到每个视图控制器。



我的自定义视图相当简单,它包含一些带有标签的UIButtons,并点击这些按钮会触发更改视图控制器视图上控件内容的操作。



合并此UIView的定义和用法的策略是什么?理想情况下,我只想从视图控制器的nib中引用此自定义视图,并让我的视图控制器响应此自定义视图的操作。



编辑:根据J.Biard的建议,我试过下面没有多少运气。



我创建了另一个基于UIView的nib文件,其中包含我的可重用视图和UIView子类.m和.h文件的内容(现在只是一些UIButton对象),然后设置然后,我添加了大部分来自J.Biard的代码(我将rect更改为50,50,100,100,左侧),然后,我添加了大部分的代码到我的新创建的类名。



<出现setDelegate out现在,因为我只是试图让它工作直观的现在,我发现[self.view addSubview:view]工作比[self addSubView:view])到viewDidLoad方法的结尾



现在我得到的是我的主视图,里面有一个黑色的方块。我在某个地方错过了一个插座,或者在UIView子类中的initWithFrame或drawRect中有一些初始化需要?

解决方案

非常简单我建议你在代码中创建一个UIView的子类,并创建这个类的实例(或者你可以使用Interface Builder来创建自定义的UIView,然后存档到NIB文件,稍后恢复使用代码)。



使用代码解决方案,您可以通过调用以下方法来创建自定义UIView的实例:

  #importMyCustomView.h

//让superview决定它应该有多大,或者根据需要设置它。
CGRect sizeRect = CGRectMake(0,0,0,0);

//创建视图的实例
MyCustomView * view = [MyCustomView alloc] initWithFrame:sizeRect];

//使用@selector()设置视图上的自定义委托或设置回调方法
[view setDelegate:self]; // self = view controller

//将视图添加到控制器...(例如:根据需要更新CGRect)
[self addSubView:view];

//不要忘记在某处释放视图;-)

此示例假设您创建一个代理协议,您的View Controller可以响应,或者您可以使用@selector动态连接事件。如果你不想在代码中创建视图的实例,你可以添加一个UIView到你的NIB文件,并在检查器窗口中设置它的类类型(命令 - > 4 - >类下拉菜单)。



如果你想做接口生成器中的一切,你可以创建你的自定义UIView和使用像 - (NSArray *)loadNibNamed:(NSString *)name owner:(id) (NSDictionary *)选项(参见NSBundle)动态加载NIB文件。



最常见的选项是创建自己的xcode自定义UI库/插件,以便您的自定义控件/视图



希望这可以澄清或消除一些重新使用控件的选项。

p>

干杯 -


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.

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?

解决方案

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

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

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.

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.

Cheers-

这篇关于使用来自多个视图控制器的自定义UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆