【问题标题】:ViewController initialized but nothing insideViewController 已初始化,但内部没有任何内容
【发布时间】:2014-06-12 20:04:47
【问题描述】:

我在我的代码中遗漏了一个明显的点,但我看不到在哪里。

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

当调用presentViewController 方法时,我可以看到我的TermsViewController 上调用了viewDidLoad 方法,但是除了黑屏之外什么都没有。

当我有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.
}

【问题讨论】:

  • 你在使用故事板吗?
  • 我刚刚编辑了我的主要帖子。我正在使用故事板,这些视图在同一个视图上。
  • 如果您通常使用故事板,您可以通过 segue stackoverflow.com/questions/8924052/… 呈现新视图
  • 检查(使用断点或 NSLog)您的视图控制器 initWithNibName: 是否被调用。如果您使用“init”初始化您的视图控制器,您将不会从情节提要中加载它,因此您的插座将不会被实例化。这就是他们为零的原因。
  • 我不想为此使用 segue(对于带有 UITextView 的视图而言,有很多限制)但最后我认为我别无选择。

标签: ios objective-c uiviewcontroller presentviewcontroller


【解决方案1】:

你需要从情节提要加载你的视图控制器,试试这个:

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

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多