ViewController已初始化但内部没有任何内容

时间:2014-04-27 09:00:29

标签: ios objective-c uiviewcontroller presentviewcontroller

我在代码中遗漏了明显的一点,但我无法看到我的代码。

TermsViewController *termsVC = [[TermsViewController alloc] init];
termsVC.signUpVC = self;
[self presentViewController:termsVC animated:YES completion:nil];

当调用方法presentViewController时,我可以看到方法viewDidLoad在我的TermsViewController上被调用,但没有任何黑色屏幕被执行。

当我termsVC.view.backgroundColor = [UIColor greenColor];时,我可以看到绿屏。当我深入研究调试器时,我可以看到我的控制器已初始化但我的组件(UITextField + UIButton)是nil。 我认为presentViewController加载了链接到控制器的视图,但最终没有。

这是我的TermsViewController:

#import "TermsViewController.h"

@interface TermsViewController ()

@end

@implementation TermsViewController
@synthesize signUpVC;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = [[NSString alloc] initWithFormat:@"Test"];
    acceptButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    // Do any additional setup after loading the view.
}

1 个答案:

答案 0 :(得分:0)

您需要从故事板加载视图控制器,试试这个:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"<YourStoryboardFileName>" 
                                                     bundle:nil];
TermsViewController *termsVC = [storyboard instantiateViewControllerWithIdentifier:@"<TermsViewController Storyboard ID>"];
termsVC.signUpVC = self;
[self presentViewController:termsVC animated:YES completion:nil];