Alright, so this is an extension to a question I asked last night. I have a little firmer grasp on how data can be passed between view controllers using various techniques. I wanted to go the MVC route, and creating a Singleton class seems the closest concept similar to MVC.
Basically I created a simple app with two View Controllers and a singleton class. I am trying to pass the value of a text field into a UILabel. For whatever reason it isn't working. This is what my code looks like.
ViewController.h
#import <UIKit/UIKit.h>
#import "Model.h"
#import "ViewController2.h"
@interface ViewController : UIViewController {
NSString *text2pass;
}
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (weak, nonatomic) IBOutlet UILabel *btn;
- (IBAction)go:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize tf = _tf;
@synthesize btn = _btn;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *tfstring = _tf.text;
NSLog(@"string = %@",tfstring);
}
- (void)viewDidUnload
{
[self setTf:nil];
[self setBtn:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (IBAction)go:(id)sender {
NSLog(@"btn pressed");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController2 *vc2 = (ViewController2 *) [storyboard instantiateViewControllerWithIdentifier:@"home"];
text2pass = _tf.text;
[self passValues];
[self presentModalViewController:vc2 animated:YES];
}
-(void) passValues {
Model *model = [Model sharedModel];
model.passedText = text2pass;
}
@end
ViewController2.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface ViewController2 : UIViewController {
NSString *passedText;
}
@property (nonatomic)NSString *passedValue;
@property (weak, nonatomic) IBOutlet UILabel *lbl;
- (IBAction)back:(id)sender;
@end
ViewController2.m
#import "ViewController2.h"
@interface ViewController2 () {
NSString *passedtext;
}
@end
@implementation ViewController2
@synthesize lbl = _lbl;
@synthesize passedValue = _passedValue;
- (void)viewDidLoad
{
// do code stuff here
NSLog(@"passedText = %@",passedText);
_lbl.text = passedText;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setLbl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (IBAction)back:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *vc = (ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"welcome"];
[self presentModalViewController:vc animated:YES];
}
@end
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject {
NSString *passedText;
}
@property (nonatomic, strong) NSString* passedText;
+ (Model *) sharedModel;
@end
Model.m
#import "Model.h"
@implementation Model
@synthesize passedText = _passedText;
static Model *sharedModel = nil;
+ (Model *) sharedModel {
@synchronized(self){
if (sharedModel == nil){
sharedModel = [[self alloc] init];
}
}
return sharedModel;
}
@end
The project can be downloaded in its entirety from here http://chrisrjones.com/files/KegCop-Test.zip
If you know why the UILabel is not displaying the text field text let me know. Oh I pretty much followed this -> http://www.youtube.com/watch?v=ZFGgMPcwYjg&feature=plcp