如何从情节提要上的视图控制器获取子视图 [英] How to obtain a subview from a view controller located on storyboard

查看:42
本文介绍了如何从情节提要上的视图控制器获取子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器,每个都有一个带有标签的UIView.我想在按下按钮时将第一个视图控制器的视图设置为第二个视图.当我执行此操作时,第二个视图控制器不会加载该视图,并且第二个视图上的UIView指针为null,因此我无法在第一个视图上对其执行任何操作.我正在做的就是这个,因为我的secondViewNew是我的secondView的属性.这可以在情节提要上完成吗?我还尝试了[self.storyboard InstantiateViewControllerWithIdentifier:@"beaconContentID"]方法.在这个项目中,secondViewNew也是UIView,并且我已经导入了所有相关的类.

I have two view controllers, and each have a UIView that has a label on it. I want to set the view from the first view controller to be the second view when I press a button. When I go to do this, the second view controller doesn't load the view and the pointer is null to my UIView on my second view, so I can't do anything with it on my first view. All I am doing is this, as my secondViewNew is a property of my secondView. Is this possible to be done on the storyboard? I have also tried the [self.storyboard instantiateViewControllerWithIdentifier:@"beaconContentID"] method of doing things. In this project, secondViewNew is a UIView as well, and I have imported all the relevant classes.

SecondViewController *secondViewControllerInstance =  [[SecondViewController alloc] init];

UIView *test = secondViewControllerInstance.secondViewNew;

NSLog(@"%@",test);

推荐答案

在分配/初始化之后,不会立即加载您的secondViewNew.

Your secondViewNew is not going to be loaded immediately after alloc/init.

仅保证在第二个视图控制器上调用 awakeFromNib 方法后实例化它.

You are only guaranteed to have it instantiated after the awakeFromNib method is called on your second view controller.

我不确定您要实现的目标,但是听起来好像您应该将视图与视图控制器分开,如果它不属于"其中一个视图.只需继承UIView并创建关联的笔尖即可.这样,您可以在需要时实例化视图.

I'm not sure what you are trying to achieve but it sounds like you should separate your view from the view controller if it doesn't "belong" to one of them. Just subclass UIView and create an associated nib. This way you can instantiate the view whenever you need it.

以下是步骤:

  • 创建UIView的新子类,即CustomView
  • 创建一个新的笔尖文件,即CustomView.xib
  • 在您的nib文件中添加视图,并将其自定义类设置为CustomView,根据需要添加子视图,等等

最后,在要使用自定义视图的视图控制器中:

Finally, in the view controller where you want to use the custom view:

CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];

这篇关于如何从情节提要上的视图控制器获取子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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