这是一个示例代码,它为两个单独的按钮更新容器视图内容。
内容是两个不同的UIViewControllers。
注意:在向ContinerView添加一个视图之前,不要忘记清除容器视图以管理内存。
.h 文件
MyViewController1 * myViewController1;
MyViewController2 * myViewController2;
@property (nonatomic, strong) IBOutlet UIView *containerView;
.m 文件
// Button-1
- (IBAction)button1_TouchUpInside:(UIButton *)sender {
for (UIView *view in [containerView subviews]) {
[view removeFromSuperview];
}
[button1 setSelected:YES];
myViewController1 = nil;
myViewController1 = [[MyViewController1 alloc]
initWithNibName:@"MyViewController1"
bundle:[NSBundle mainBundle]];
[self.containerView addSubView:myViewController1.view];
}
- (IBAction)button2_TouchUpInside:(UIButton *)sender {
for (UIView *view in [containerView subviews]) {
[view removeFromSuperview];
}
[button2 setSelected:YES];
myViewController2 = nil;
myViewController2 = [[MyViewController1 alloc]
initWithNibName:@"MyViewController1"
bundle:[NSBundle mainBundle]];
[self.containerView addSubView:myViewController2.view];
}
希望这将有助于解决您的问题。