Note that there are some explanatory texts on larger screens.

plurals
  1. POObj-C - How to pass data between viewcontrollers using a singleton?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p><strong>ViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #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 </code></pre> <p><strong>ViewController.m</strong></p> <pre><code> #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 </code></pre> <p><strong>ViewController2.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "ViewController.h" @interface ViewController2 : UIViewController { NSString *passedText; } @property (nonatomic)NSString *passedValue; @property (weak, nonatomic) IBOutlet UILabel *lbl; - (IBAction)back:(id)sender; @end </code></pre> <p><strong>ViewController2.m</strong></p> <pre><code>#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 </code></pre> <p><strong>Model.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Model : NSObject { NSString *passedText; } @property (nonatomic, strong) NSString* passedText; + (Model *) sharedModel; @end </code></pre> <p><strong>Model.m</strong></p> <pre><code>#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 </code></pre> <p>The project can be downloaded in its entirety from here <a href="http://chrisrjones.com/files/KegCop-Test.zip" rel="nofollow">http://chrisrjones.com/files/KegCop-Test.zip</a></p> <p>If you know why the UILabel is not displaying the text field text let me know. Oh I pretty much followed this -> <a href="http://www.youtube.com/watch?v=ZFGgMPcwYjg&amp;feature=plcp" rel="nofollow">http://www.youtube.com/watch?v=ZFGgMPcwYjg&amp;feature=plcp</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload