以下是一個示例代碼,它更新了兩個單獨按鈕的容器視圖內容。
和內容是兩個不同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];
}
希望這將有助於解決您的問題。
請標記答案爲接受,如果它幫助你。 – Mrunal