xcode 将 XIB 中定义的 UIView 加载到 UIView 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13211407/
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 defined in XIB into UIView
提问by itsame69
following a post presented here, I tried to load a view defined in a XIB file into a view, currently being displayed. Basically, what I want to do is to replace the middle view (see screenshot below) with a different view when the user clicks on the button.
按照此处介绍的帖子,我尝试将在 XIB 文件中定义的视图加载到当前正在显示的视图中。基本上,我想要做的是当用户单击按钮时用不同的视图替换中间视图(见下面的截图)。


This is the simple view I want to load into the middle view:
这是我想加载到中间视图的简单视图:


And this is the source code of the ViewController:
这是 ViewController 的源代码:
ViewController.h #import
ViewController.h #import
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *loadxibButton;
@property (strong, nonatomic) IBOutlet UIView *middleView;
- (IBAction)buttonClicked:(id)sender;
@end
ViewController.m #import "ViewController.h"
ViewController.m #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)buttonClicked:(id)sender {
NSLog(@"click...");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
@end
When I click on the button, nothing happens, i.e. the new view will not be displayed. Can anybody please help?
当我单击按钮时,什么也没有发生,即不会显示新视图。有人可以帮忙吗?
Thanks a lot Christian
非常感谢基督徒
采纳答案by Phillip Mills
Your view controller has a viewproperty which, presumably, has three subviews (left, middle, and right). Instead of just setting a middleViewproperty in your controller, your strategy should be to update the primary view's subviews array (removing the one that's to be replaced and inserting the new one).
您的视图控制器有一个view属性,大概有三个子视图(左、中和右)。不是仅仅middleView在控制器中设置一个属性,您的策略应该是更新主视图的子视图数组(删除要替换的子视图并插入新的子视图)。
回答by itsame69
Phillip, thanks a lot!
菲利普,非常感谢!
Instead of
代替
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
I now use this code:
我现在使用这个代码:
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
nibView.frame = CGRectMake(261, 0, 532, 748);
[[[self view] viewWithTag:2] removeFromSuperview];
[[self view] addSubview:nibView];
}
Is this want you meant, i.e. is this like "best practice"?
这是您的意思,即这是否类似于“最佳实践”?
Bye Christian
再见基督徒

