-10

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

ipatch
  • 3,933
  • 8
  • 60
  • 99

2 Answers2

5

Your addressing, and memory management is just plain... off. Firstly, there's absolutely no reason to create a singleton for this, but that's beside the point here.

Secondly, when declaring properties, (atomic, assign) is defaulted to if not otherwise specified, which means your string:

@property (nonatomic)NSString *passedValue;

is weak sauce, ripe for deallocation and destruction at a moments notice. Declare it copy, strong, or retain.

Thirdly, there's absolutely no reference to your singleton in the pushed view controller, yet you seem to have the belief that objects that are named the same in different classes retain their value (especially when #import'ed). Not so. You need to reference your singleton and pull the value of [Model sharedModel].passedText into that text field.

In fact, I fixed your sample in two lines:

//ViewController2.m
#import "ViewController2.h"

//actually import the singleton for access later
#import "Model.h"

@interface ViewController2 () {
    NSString *passedtext;
}
@end
@implementation ViewController2
@synthesize lbl = _lbl;
@synthesize passedValue = _passedValue;
- (void)viewDidLoad
{

 // do code stuff here
    NSLog(@"passedText = %@",passedText);
    //actually reference the singleton this time
    _lbl.text = [Model sharedModel].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

Which yields this:

Fixed the code

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
CodaFi
  • 43,043
  • 8
  • 107
  • 153
1

I wouldn't recommend using a Singleton as a good way to pass data around your application. Most apps are simple enough that this kind of central access is not necessary, and it usually creates a maintenance nightmare... but I don't think the fact that you're using a Singleton is actually important to getting your code working.

Assuming you have access to the data in ViewController1, in your case through the a Singleton instance of Model (which needs a more descriptive name), then all you have to do is pass through the data to ViewController2 when it is created and presented, which eliminates the need for a Singleton at all.

Once you create the controller, set the data you need, and then present the view controller - which is basically what you're doing anyway.

As to why it's not working: Is the view controller being presented, just not with the correct data? Or is there actually an issue presenting the controller at all? I would set a breakpoint in the go: action of ViewController1, make sure the data you expect is in the textfield, correctly populates the Model and that the value is correctly pulled out of the Model in ViewController2.

Unless you've removed some of the code, it looks like you correctly populate the Model property in ViewController1, but in ViewController2 you refer to a local ivar passedTextrather than pulling it from the model.

On a separate note, the way to go back from a presented modal view controller is usually to dismiss that controller, not to re-create the initial controller and present that over the top.

Stew
  • 1,901
  • 10
  • 9