开发者

Display different iphone views depending on logged in status

I want to display a login view to my users if the开发者_如何学Goy are not logged in and the main view if they are.

In my header file I define a variable to hold the logged in status

#define loggedIn 0

I figure I should then reference this in the initWithNibName method and then decide which nib to load.

Is the right way of doing it?

If so can someone help me out with the exact code?

Thanks for any help


I don't think you understand how to use define directives.

All this line...

#define loggedIn 0

... does it cause the compiler to substitute a zero everywhere in the code that the text "loggedIn" appears.

So, if you have code that says:

if (loggedIn) {
    //... load one nib
}else{
    // .... load another nib
}

The compiler turns that into:

if (0) {
    //... load one nib
}else{
    // .... load another nib
} 

In this case, zero will evalute to false and the second nib will always be loaded.

These types of defines are only used in this way in development so you can force the app into a specific state. For example, if you wanted to test the second nib repeatedly you would define "loggedIn" to zero and if you wanted to test the first you would define "loggedIN" as 1.

What you need to do is to do a test of some kind to see if the user is logged in. I don't know what that test would be as it varies on what your logging into. Then depending on the results of that test, you would load one nib or the other.

The define directive wouldn't have anything to do with it.


#define is not what you want, as explained by TechZen. You should rather use an int or a BOOL to do this.

For example:

BOOL loggedIn = NO;

- (void) login {
    // Check login details, if correct continue, if not, break.
    BOOL loggedIn = YES
}

Then you could use that in another function;

if (loggedIn == YES) {
    NSLog(@"Logged in!");
    // Load nib
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜