Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created October 30, 2012 22:05
Show Gist options
  • Save ChrisRisner/3983381 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3983381 to your computer and use it in GitHub Desktop.
iOS Day 10
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *info;
@end
@implementation AppDelegate
-(void)doSomethingWithString:(NSString*)parameter {
self.info = parameter;
}
-(NSString*)getInfo {
return self.info;
}
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *info;
-(void)doSomethingWithString:(NSString*)parameter;
-(NSString*)getInfo;
@end
@interface MySingleton : NSObject{
NSString *info;
}
+(MySingleton*) getInstance;
-(void)doSomethingWithString:(NSString*)parameter;
-(NSString*)getInfo;
@end
#import "MySingleton.h"
@implementation MySingleton
static MySingleton *singletonInstance;
+ (MySingleton*)getInstance{
if (singletonInstance == nil) {
singletonInstance = [[super alloc] init];
}
return singletonInstance;
}
-(void)doSomethingWithString:(NSString*)parameter {
info = parameter;
}
-(NSString*)getInfo {
return info;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)tappedSaveData:(id)sender;
- (IBAction)tappedLoadData:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lblInfo;
@end
- (IBAction)tappedSaveData:(id)sender {
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
delegate.info = @"Some text!";
}
- (IBAction)tappedLoadData:(id)sender {
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[self.lblInfo setText:delegate.info];
}
- (IBAction)tappedSaveData:(id)sender {
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[delegate doSomethingWithString:@"some text"];
}
- (IBAction)tappedLoadData:(id)sender {
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[self.lblInfo setText:[delegate getInfo]];
}
- (IBAction)tappedSaveData:(id)sender {
// AppDelegate *delegate = [UIApplication sharedApplication].delegate;
// [delegate doSomethingWithString:@"some text"]
MySingleton* singleton = [MySingleton getInstance];
[singleton doSomethingWithString:@"some text"];
}
- (IBAction)tappedLoadData:(id)sender {
// AppDelegate *delegate = [UIApplication sharedApplication].delegate;
// [self.lblInfo setText:[delegate getInfo]];
MySingleton* singleton = [MySingleton getInstance];
[self.lblInfo setText:[singleton getInfo]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment