photo from: google+ |
October 5, 2015 - 22nd Day As An iOS Developer With No Experience
Alternate title of this post: Determine the LogInMode iOS Using Enum and Static Object.
MONDAY - I learned about Static. Thank you so much sir G. I learned so many stuffs from him. In my 2nd project (take note that I'm far from done in my 1st project, sad.) there are 2 ways to log in. Log in using your email, and log in via Facebook. Now, if you choose either of the two, you should be redirected to its respective next screen. How to do that without passing so much values to each View Controllers?
Answer: Use Static Variable. I will explain it further here. In your AppDelegate.h file, you declare an enum. This will be accessed by your two view controllers (the Login email, and the Login Via Facebook).
Put this code at the top of your interface in AppDelegate.h
typedef enum { LoginModeEmail, LoginModeFacebook } LoginMode;
Next, make a property (property makes the variable or object be public and be accessible by all classes). Properties are to be put inside the Interface or before @end of interface.
@property (nonatomic) LoginMode AppLoginMode;
Then, make a method, a plus sign indicates static method. Again, methods are to be declared above @end of interface.
+ (AppDelegate *) getInstance;
The last step in declaring stuffs in our AppDelegate is to put a static method, or implement the static method we declared in our AppDelegate.h file, put the following code implementation in your AppDelegate.m file
static AppDelegate *instance; + (AppDelegate *) getInstance { return instance; }
Okay, now we're done in declaring stuffs in our AppDelegate. In your button event, you put the data in your AppLogInMode enum. See the code below, I have this code in my Log In With Facebook Button.
[AppDelegate getInstance].AppLoginMode = LoginModeFacebook;
And in my log in using email button:
[AppDelegate getInstance].AppLoginMode = LoginModeEmail;
We're almost done, last we wanna know which Mode was set to our delegate. It's up to you where you should put the condition statement to check the mode, example, if the Login Mode is Email, then do the following.
if ([AppDelegate getInstance].AppLoginMode == LoginModeEmail) { // todo: loginmodemail direction }
The lesson we learned here is that we do not have to instantiate the AppDelegate to get the data from it. Please subscribe.
Post a Comment