viewWillAppear 中的奇怪行为

发布于 2024-12-14 10:30:36 字数 1282 浏览 1 评论 0原文

我有一个 TabBar 控制器,其中有一些选项卡栏项目。

用户第一次点击选项卡栏项目时,我希望打开一个警报视图,以便用户可以阅读一些小说明提示。

我有一个全局变量(比如 CONFIG),它保存一些布尔值(CONFIG.tip1AlreadySeen、CONFIG.tip1AllreadySeen 等)。所有这些布尔值都被初始化为NO。

当用户点击选项卡栏项目时,将执行其视图控制器中的 viewWillAppear 方法。在这种方法中,我放置了如下代码:

-(void) viewVillAppear: (BOOL) animated {
    extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used

    [super viewWillAppear: animated];

    if(CONFIG.tip1AlreadySeen == NO) {
        CONFIG.tip1AlreadySeen = YES;

        // code for showing the alertview
    }    
}

奇怪的是,这段代码在一个视图控制器中完美工作,但在另一个视图控制器中不起作用。

通过一些调试,我发现在另一个视图控制器中执行了代码,但分配 CONFIG.tipAlreadySeen = YES 并没有修改 CONFIG.tipAlreadySeen 的实际值。这个值仍然是NO。难以置信!!!

一个小小的解决方法是使用 viewDidAppear 方法来更改值:

-(void) viewVillAppear: (BOOL) animated {
    extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used

    [super viewWillAppear: animated];

    if(CONFIG.tip1AlreadySeen == NO) {
        // code for showing the alertview
    }    
}

-(void) viewDidAppear: (BOOL) animated {
    extern CONFIG;
    CONFIG.tip1AlreadySeen = YES;
}

...但我真的不明白发生了什么!你们中有人可以解释这种行为吗?

提前致谢!

马可

I have a TabBar Controller with some tab bar item in it.

The first time that a user tap on a tab bar item, I want that a alertview is opened, so that the user can read some little instruction tips.

I have a global variable (say CONFIG), that hold some boolean valeus (CONFIG.tip1AlreadySeen, CONFIG.tip1AllreadySeen, etc.). All these boolean values are initializated to NO.

When the user tap a tab bar item, the viewWillAppear method in its viewcontroller is executed. In this method I put a code like this one:

-(void) viewVillAppear: (BOOL) animated {
    extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used

    [super viewWillAppear: animated];

    if(CONFIG.tip1AlreadySeen == NO) {
        CONFIG.tip1AlreadySeen = YES;

        // code for showing the alertview
    }    
}

The strange thing is that this piece of code works perfectly in one viewcontroller but doesn't work in one another.

With some debug, I fidd out that in the another viewcontroller the code is executed but the assigment CONFIG.tipAlreadySeen = YES doesn't modify the actual value of CONFIG.tipAlreadySeen. This value is still NO. Unbelievable!!!

A little workaround was using the viewDidAppear method for changing the value:

-(void) viewVillAppear: (BOOL) animated {
    extern CONFIG; // <- it's not the actual code but it indicates that a global variable must be used

    [super viewWillAppear: animated];

    if(CONFIG.tip1AlreadySeen == NO) {
        // code for showing the alertview
    }    
}

-(void) viewDidAppear: (BOOL) animated {
    extern CONFIG;
    CONFIG.tip1AlreadySeen = YES;
}

...But I really did not understand what happened!!! Someone of you could explain this behaviour?

Thanks in advance!

Marco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2024-12-21 10:30:36

为什么它必须是全局的并且不包含在视图控制器本身中?只需在视图控制器上切换一个简单的 BOOL @property 即可。并且,为了在应用程序的多次运行中保持这种持久性,请将结果保存到 NSUserDefaults 中,然后在每次初始化视图控制器时检查它。

Why must this be global and not contained in the view controller itself? Just a simple BOOL @property on your view controller that is toggled. And, to maintain this persistent across multiple runs of your application, save out the result to NSUserDefaults, which you in turn check each time you init your view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文