xcode 使用 Nib 和 UIView 类加载 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16390049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load UIView with Nib and UIView class
提问by rostgaard
I've tried this class
我试过这门课
https://github.com/autresphere/ASDepthModal
https://github.com/autresphere/ASDepthModal
i want it to popup like it does but i want to be able to set the labels programmatically, since i need the to change depending on what day it is. I'm using storyboard, so i've created a .xib and uiview.h and uiview.m. In my main UIViewController i have:
我希望它像它一样弹出,但我希望能够以编程方式设置标签,因为我需要根据日期进行更改。我正在使用故事板,所以我创建了一个 .xib 和 uiview.h 以及 uiview.m。在我的主 UIViewController 中,我有:
xibContents = [[NSBundle mainBundle] loadNibNamed:@".xib" owner:self options:nil];
testView = [xibContents lastObject];
in my .xib i have set the file owner to my uiview class, this create a problem: NSUnknownKeyException When i set the uiiew inside my .xib to my uiview class the application will load and i can open it just like it should, but i'm not able to change the state of the label programmatically? I'm complety lost here!
在我的 .xib 中,我将文件所有者设置为我的 uiview 类,这会产生一个问题:NSUnknownKeyException 当我将 .xib 中的 uiiew 设置为我的 uiview 类时,应用程序将加载,我可以像它应该的那样打开它,但是我'不能以编程方式更改标签的状态?我完全迷失在这里!
采纳答案by rostgaard
Actually got this to work. I took the wrong approach. I found it simpler to just create the view and populate it with background image and labels when the button got clicked. would have been simple to do it in a UI designer, but this wasn't that hard actually.
实际上得到了这个工作。我采取了错误的方法。我发现创建视图并在单击按钮时用背景图像和标签填充它更简单。在 UI 设计师中这样做会很简单,但这实际上并没有那么难。
Thanks to the people who helped me :)
感谢帮助我的人:)
回答by James
Typically speaking, UIViews do not have access to IBOutlets. Apple kind of intended xibs to only be assigned to UIViewControllers.
通常来说,UIViews 无权访问 IBOutlets。Apple 打算将 xib 分配给 UIViewControllers。
However, you can load a view from a xib in two ways:
但是,您可以通过两种方式从 xib 加载视图:
1) Create an extra xib to use in your UIViewController. Set the File's Owner to your view controller, and the class name of the view to your custom view class. In interface builder, this is under "custom class". You can set the view as a IBOutlet, and iOS will create an instance of your custom class when your UIViewController loads the xib and sets itself as owner (like you tried above, but only from within a controller class)
1) 创建一个额外的 xib 以在您的 UIViewController 中使用。将 File's Owner 设置为您的视图控制器,并将视图的类名称设置为您的自定义视图类。在界面构建器中,这是在“自定义类”下。您可以将视图设置为 IBOutlet,当您的 UIViewController 加载 xib 并将自身设置为所有者时,iOS 将创建您的自定义类的实例(就像您在上面尝试过的那样,但只能从控制器类中)
2) Load a xib in a UIView class, and set self to the resultant object:
2) 在 UIView 类中加载一个 xib,并将 self 设置为结果对象:
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, 1024, 352)];
if (self) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"TCSNumberKeyPad" owner:self options:nil];
[[nib objectAtIndex:0] setFrame:CGRectMake(0, 0, 1024, 352)];
self = [nib objectAtIndex:0];
}
return self;
}
In either case, you will need to retrieve your label via code rather than IBOutlet properties. You can find your label in the subviews property:
在任何一种情况下,您都需要通过代码而不是 IBOutlet 属性来检索您的标签。您可以在 subviews 属性中找到您的标签:
UILabel* myLabel = [self.subviews objectAtIndex:0];
UILabel* myLabel = [self.subviews objectAtIndex:0];
回答by isaach1000
The file's owner should be the view controller, not the view itself. The view can have outlets to the labels. The view should be set to your custom class in your nib.
文件的所有者应该是视图控制器,而不是视图本身。视图可以有标签的出口。该视图应设置为您笔尖中的自定义类。