无法显示子视图(iOS视图容器)

时间:2015-04-01 11:41:40

标签: ios objective-c uicontainerview

在我的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

FirstViewController.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

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

Main View Controller First View Controller

1 个答案:

答案 0 :(得分:0)

作为创建FirstViewController的一部分,您正在调用生成裸[super init]的{​​{1}}。从您的图表中,您似乎想要从故事板中加载UIViewController

我建议使用FirstViewController创建控制器的序列,然后在控制器中设置要用作标签的字符串属性,最后在控制器视图加载时将字符串分配给标签。