无法显示子视图(iOS 视图容器)
Unable to show child view (iOS view container)
在我的 iOS 应用程序中,我有一个带有三个按钮的主视图控制器,其工作方式类似于选项卡栏:当我单击其中一个按钮时,将调用一个新的视图控制器。
我试图通过容器视图容器来实现它,所以我尝试按照本指南 (http://www.thinkandbuild.it/working-with-custom-container-view-controllers/) 并在主控制器的 viewDidLoad 中调用 presentDetailController 方法。
实际上没有显示任何视图:有人可以帮我弄清楚为什么吗?谢谢。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btnOne;
@property (weak, nonatomic) IBOutlet UIButton *btnTwo;
@property (weak, nonatomic) IBOutlet UIButton *btnThree;
@property (weak, nonatomic) IBOutlet UIView *detailView;
- (IBAction)click:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@property UIViewController *currentDetailViewController;
@end
@implementation ViewController
@synthesize btnOne, btnTwo, btnThree;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FirstViewController *fvc = [[FirstViewController alloc]initWithString:@"I'm the first Controller!"];
[self presentDetailController:fvc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender
{
// button selection stuff
[self addDetailController:sender];
}
- (void)presentDetailController:(UIViewController*)detailVC{
//0. Remove the current Detail View Controller showed
if(self.currentDetailViewController){
[self removeCurrentDetailViewController];
}
//1. Add the detail controller as child of the container
[self addChildViewController: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.currentDetailViewController = detailVC;
//4. Complete the add flow calling the function didMoveToParentViewController
[detailVC didMoveToParentViewController:self];
}
- (void)removeCurrentDetailViewController{
//1. Call the willMoveToParentViewController with nil
// This is the last method where your detailViewController can perform some operations before neing removed
[self.currentDetailViewController willMoveToParentViewController:nil];
//2. Remove the DetailViewController's view from the Container
[self.currentDetailViewController.view removeFromSuperview];
//3. Update the hierarchy"
// Automatically the method didMoveToParentViewController: will be called on the detailViewController)
[self.currentDetailViewController removeFromParentViewController];
}
- (CGRect)frameForDetailController{
// newFrame's height should be currentFrame's height minus buttons' height
CGRect detailFrame = CGRectMake(0, 0, self.detailView.bounds.size.width, self.detailView.bounds.size.height-self.btnOne.frame.size.height);
return detailFrame;
}
- (void)addDetailController:(id)sender {
FirstViewController *detailVC = [[FirstViewController alloc]initWithString:@"First button clicked"];
[self presentDetailController:detailVC];
}
@end
第一ViewController.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *btnOne;
@property (weak, nonatomic) IBOutlet UIButton *btnTwo;
@property (weak, nonatomic) IBOutlet UIButton *btnThree;
- (id)initWithString:(NSString*)string;
@end
第一个ViewController.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
作为创建 FirstViewController
的一部分,您正在调用 [super init]
,它会生成一个空 UIViewController
。从你的图表来看,你似乎想从故事板中加载 FirstViewController
。
我建议使用 instantiateViewControllerWithIdentifier:
创建控制器的顺序,然后在控制器中设置要用作标签的字符串 属性,最后将字符串分配给标签当控制器的视图加载时。
在我的 iOS 应用程序中,我有一个带有三个按钮的主视图控制器,其工作方式类似于选项卡栏:当我单击其中一个按钮时,将调用一个新的视图控制器。 我试图通过容器视图容器来实现它,所以我尝试按照本指南 (http://www.thinkandbuild.it/working-with-custom-container-view-controllers/) 并在主控制器的 viewDidLoad 中调用 presentDetailController 方法。
实际上没有显示任何视图:有人可以帮我弄清楚为什么吗?谢谢。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btnOne;
@property (weak, nonatomic) IBOutlet UIButton *btnTwo;
@property (weak, nonatomic) IBOutlet UIButton *btnThree;
@property (weak, nonatomic) IBOutlet UIView *detailView;
- (IBAction)click:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@property UIViewController *currentDetailViewController;
@end
@implementation ViewController
@synthesize btnOne, btnTwo, btnThree;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FirstViewController *fvc = [[FirstViewController alloc]initWithString:@"I'm the first Controller!"];
[self presentDetailController:fvc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender
{
// button selection stuff
[self addDetailController:sender];
}
- (void)presentDetailController:(UIViewController*)detailVC{
//0. Remove the current Detail View Controller showed
if(self.currentDetailViewController){
[self removeCurrentDetailViewController];
}
//1. Add the detail controller as child of the container
[self addChildViewController: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.currentDetailViewController = detailVC;
//4. Complete the add flow calling the function didMoveToParentViewController
[detailVC didMoveToParentViewController:self];
}
- (void)removeCurrentDetailViewController{
//1. Call the willMoveToParentViewController with nil
// This is the last method where your detailViewController can perform some operations before neing removed
[self.currentDetailViewController willMoveToParentViewController:nil];
//2. Remove the DetailViewController's view from the Container
[self.currentDetailViewController.view removeFromSuperview];
//3. Update the hierarchy"
// Automatically the method didMoveToParentViewController: will be called on the detailViewController)
[self.currentDetailViewController removeFromParentViewController];
}
- (CGRect)frameForDetailController{
// newFrame's height should be currentFrame's height minus buttons' height
CGRect detailFrame = CGRectMake(0, 0, self.detailView.bounds.size.width, self.detailView.bounds.size.height-self.btnOne.frame.size.height);
return detailFrame;
}
- (void)addDetailController:(id)sender {
FirstViewController *detailVC = [[FirstViewController alloc]initWithString:@"First button clicked"];
[self presentDetailController:detailVC];
}
@end
第一ViewController.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *btnOne;
@property (weak, nonatomic) IBOutlet UIButton *btnTwo;
@property (weak, nonatomic) IBOutlet UIButton *btnThree;
- (id)initWithString:(NSString*)string;
@end
第一个ViewController.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
作为创建 FirstViewController
的一部分,您正在调用 [super init]
,它会生成一个空 UIViewController
。从你的图表来看,你似乎想从故事板中加载 FirstViewController
。
我建议使用 instantiateViewControllerWithIdentifier:
创建控制器的顺序,然后在控制器中设置要用作标签的字符串 属性,最后将字符串分配给标签当控制器的视图加载时。