以下は、2 つの別個のボタンのコンテナ ビュー コンテンツを更新する 1 つのサンプル コードです。
そして内容は2つの異なるUIViewControllersです。
注: ContinerViewに 1 つのビューを追加する前に、コンテナー ビューをクリアしてメモリを管理することを忘れないでください。
.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];
}
これが問題の解決に役立つことを願っています。