我目前正在开发一个在布局底部有三个按钮的应用程序(出于自定义原因,我无法使用TabBar控制器或分段控制)。
当我单击其中一个按钮时,新视图应显示并继承按钮栏及其状态(按钮可处于选定或未选定状态)。
我对iOS开发完全不熟悉,所以我尝试遵循本指南(http://www.thinkandbuild.it/working-with-custom-container-view-controllers/),但问题是当我点击一个按钮时,子视图没有出现。
ContainerViewController.h
@interface ContainerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *thirdButton;
@property (weak, nonatomic) IBOutlet UIView *detailView;
- (IBAction)click:(id)sender;
@end
ContainerViewController.m
#import "ContainerViewController.h"
#import "FirstViewController.h"
@interface ContainerViewController ()
@property UIContainerViewController *currentDetailContainerViewController;
@end
@implementation ContainerViewController
@synthesize firstButton, secondButton, thirdButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender
{
// switch buttons state
// ...
[self addDetailController:sender];
}
- (void)presentDetailController:(UIContainerViewController*)detailVC{
//0. Remove the current Detail View Controller showed
if(self.currentDetailContainerViewController){
[self removeCurrentDetailContainerViewController];
}
//1. Add the detail controller as child of the container
[self addChildContainerViewController:detailVC];
//2. Define the detail controller's view size
detailVC.view.frame = [self frameForDetailController];
//3. Add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller
[self.detailView addSubview:detailVC.view];
self.currentDetailContainerViewController = detailVC;
//4. Complete the add flow calling the function didMoveToParentContainerViewController
[detailVC didMoveToParentContainerViewController:self];
}
- (void)removeCurrentDetailContainerViewController{
//1. Call the willMoveToParentContainerViewController with nil
// This is the last method where your detailContainerViewController can perform some operations before neing removed
[self.currentDetailContainerViewController willMoveToParentContainerViewController:nil];
//2. Remove the DetailContainerViewController's view from the Container
[self.currentDetailContainerViewController.view removeFromSuperview];
//3. Update the hierarchy"
// Automatically the method didMoveToParentContainerViewController: will be called on the detailContainerViewController)
[self.currentDetailContainerViewController removeFromParentContainerViewController];
}
- (CGRect)frameForDetailController{
CGRect detailFrame = self.detailView.bounds;
return detailFrame;
}
- (void)addDetailController:(id)sender {
FirstViewController *detailVC = [[FirstViewController alloc]initWithString:@"First view"];
[self presentDetailController:detailVC];
}
@end
FirstViewController.h
#import <UIKit/UIKit.h>
@class ContainerViewController;
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
- (id)initWithString:(NSString*)string;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController (){
NSString *text;
}
@end
@implementation FirstViewController
- (id)initWithString:(NSString*)string {
self = [super init];
if(self){
text = string;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.label.text = text;
}
@end