在initWithNibName之前调用ViewDidLoad? [英] ViewDidLoad being called before initWithNibName?

查看:93
本文介绍了在initWithNibName之前调用ViewDidLoad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了最奇怪的错误,我希望有人可以帮助我。
这是我创建视图控制器并将其推送到navigationController时的代码。
问题是将随机变量传递给新的视图控制器。我尝试在init方法中传递它,并使用下面注释的行传递它。

I'm having the strangest error, and i hope someone could help me. Here is the code when I create a view controller and push it to navigationController. the problem is passing the random variable to the new view controller. I tried passing it in the init method and also passing it with the line commented below.

    MultipleBet *multipleBet = [[MultipleBet alloc] initWithMaxNumber:numbers andMaxStars:stars andRandom:self.random];
    NSLog(@"RANDOM1: %d", self.random);
    //[multipleBet setRandom:self.random];

    UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] init] autorelease];
    backButton.title = @"Voltar";
    self.navigationItem.backBarButtonItem = backButton;
    [self.navigationController pushViewController:multipleBet animated:YES];

    [multipleBet release];

然而,当我在MultipleBet的viewDidLoad中访问随机变量时,它始终为FALSE。

However when i access the random variable in the viewDidLoad of the MultipleBet, it's always FALSE.

这里是MultipleBet的代码:

here is the code of the MultipleBet:

- (id)initWithMaxNumber:(int)maxNumbers andMaxStars:(int)maxStars andRandom:(BOOL)isRandom {
    self = [super initWithNibName:@"MultipleBet" bundle:[NSBundle mainBundle]];
    ...

    self.random = isRandom;
    NSLog(@"RANDOM2: %d", self.random);
    NSLog(@"RANDOM2.1: %d", isRandom);

return self;
}

这里是viewDidLoad的代码:

and here is the code of the viewDidLoad:

- (void)viewDidLoad {
   [super viewDidLoad];


    NSLog(@"RANDOM2.2: %d", self.random);

}

我声明变量和属性如下:

i declare the variable and property like this:

BOOL random;
@property (nonatomic) BOOL random;

且输出始终为:

RANDOM2.2:0

RANDOM2:1

RANDOM2.1:1

RANDOM1:1

and the output is always:
RANDOM2.2: 0
RANDOM2: 1
RANDOM2.1: 1
RANDOM1: 1

为什么是来自viewDidLoad的NSLog是否在所有其他之前输出?它应该是最后一个......可能是因为我的自定义init方法?我打电话给[super init],所以它应该不是问题...

Why is the NSLog from the viewDidLoad being outputted before all the others? it should be the last... Could it be because of my custom init method? I call the [super init], so it shouldn't be a problem...

推荐答案

viewDidLoad init * 方法不能保证一个接一个地执行。 init * 方法中的代码可能会导致视图加载,因此即使<$ c,也会调用 viewDidLoad $ c> init * 方法尚未完成。

viewDidLoad and init* methods are not guaranteed to execute one after the other. Code in your init* methods may cause the view to load, hence viewDidLoad will be called even if the init* method has not finished.

很可能,您省略的部分中的某些代码导致视图加载。如果您将向我们展示这一部分,也许我们可以指出它。或者,您也可以将 self.random = isRandom; 行移动到中的第一行if if(self)阻止,看看是否有效。这样,在您分配 self.random 之后,将执行导致视图加载的任何内容。

Most likely, some code in the part you omitted is causing the view to load. If you will show us that part, maybe we can point it out. Or, you can also move the self.random = isRandom; line as the first line inside your if (self) block and see if that works out. This way, whatever is causing the view to load will be executed after you have assigned self.random.

- (id)initWithMaxNumber:(int)maxNumbers andMaxStars:(int)maxStars andRandom:(BOOL)isRandom {
    self = [super initWithNibName:@"MultipleBet" bundle:[NSBundle mainBundle]];
    if (self) {
        // do this first
        self.random = isRandom;
        NSLog(@"RANDOM2: %d", self.random);
        NSLog(@"RANDOM2.1: %d", isRandom);

        // do the other code after
        ...
    }

    return self;
}

这篇关于在initWithNibName之前调用ViewDidLoad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆