Wednesday 19 March 2014

AppDelegate Method - Store a Variable (Singleton)

AppDelegate.h :
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
 NSString *name;
}
@property(strong,nonatomic)NSString *name;

@end

AppDelegate.m :
 //Nothing Do

SomeFile.h :
#import "AppDelegate.h"
@interface SomeFile : UIViewController
{
    
}
-(IBAction)save:(id)sender;
-(IBAction)retrive:(id)sender;
@end

SomeFile.m :
-(IBAction)save:(id)sender
{
      AppDelegate *del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
      [del setName:txt_name.text];
}

-(IBAction)retrive:(id)sender
{
      AppDelegate *del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
     if (del.name==nil)
     {
           // Login page is showed
     }
     else
     {
           NSString *name = del.name; 
     }
}

No comments: