xcode 将 XIB 加载到子视图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12655475/
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 XIB into a subview
提问by Neil
I have been looking all over fo information on how to do this but have found nothing that I understand.
我一直在寻找有关如何执行此操作的信息,但没有发现任何我理解的内容。
Basically I want to be able (from ViewController.xib) to load another XIB file on top of that view in a subview. This new subview should be manipulatable, so it can be moved around with multitouch gestures and/or swipe gestures.
基本上我希望能够(从 ViewController.xib)在子视图中的该视图之上加载另一个 XIB 文件。这个新的子视图应该是可操作的,所以它可以通过多点触控手势和/或滑动手势来移动。
I was trying to add a "view" object from ViewController, then load the other XIB into that subview object.
我试图从 ViewController 添加一个“视图”对象,然后将另一个 XIB 加载到该子视图对象中。
Hopefully someone can help. Thanks!
希望有人可以提供帮助。谢谢!
回答by Adis
You should be able to accomplish what you want by making an instance of the other class in your ViewController, and then adding it as a subview to the current view controller.
您应该能够通过在您的 中创建另一个类的实例ViewController,然后将其作为子视图添加到当前视图控制器来完成您想要的。
MyOtherViewController *movc = [[MyOtherViewController alloc] init];
[self.view addSubview:movc.view];
As for handling the gestures, you could either handle them in the MyOtherViewControllerclass, or make a container view and handle them it in your ViewController. Don't forget that they are subviews, and that any movement should be relative to their superviews.
至于处理手势,您可以在MyOtherViewController类中处理它们,也可以创建一个容器视图并在您的ViewController. 不要忘记它们是子视图,任何移动都应该与它们的父视图相关。
回答by Perfect Brains
The above code is almost right but except
上面的代码几乎是正确的,但除了
UIView *newView = [[NSBundle mainBundle] loadNibNamed:@"MyNewXib" owner:self options:nil];
it should be
它应该是
UIView *newView = [[[NSBundle mainBundle] loadNibNamed:@"MyNewXib" owner:self options:nil] objectAtIndex:0];
and in a view controller simply you can add
在视图控制器中,您只需添加
[self.view addSubview:newView];
回答by danh
You can define the subview in a xib by creating a xib (select File New... "user interface", "view").
您可以通过创建 xib 来定义 xib 中的子视图(选择 File New...“user interface”、“view”)。
You can load that view like this:
您可以像这样加载该视图:
UIView *newView = [[NSBundle mainBundle] loadNibNamed:@"MyNewXib" owner:self options:nil];
That new view can now be treated like any other subview:
现在可以像对待任何其他子视图一样对待该新视图:
// assign it to a property
@property (strong, non atomic) UIView *viewFromXib;
@sythesize viewFromXib=_viewFromXib;
self.viewFromXib = newView;
// add it to your hierarchy
self.viewFromXib.frame = CGRectMake( /* this will start out 0,0 the size from the xib */ );
[self.view addSubview:self.viewFromXib];
// add gesture recognizer
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.viewFromXib addGestureRecognizer:tapGR];
... and so on
... 等等

