Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:13:26+00:00 2026-06-07T07:13:26+00:00

Alright, so this is an extension to a question I asked last night. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:13:27+00:00Added an answer on June 7, 2026 at 7:13 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I know this is more than likely a amateur question but I have
Alright, this problem is a little complicated, so bear with me. I have a
Alright this is a follow-up to my last question: JTable: Changing cell background when
Alright this is my first day with JQuery so have fun with these functions
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, this specific layout is just annoying me. And can't seem to find a
Alright, this is a very simple question. I just installed Tomcat 6 on my
Alright this probably is the worst error I have found ever. I have two
Alright so this is an odd one... I have an application built in ColdFusion
Alright i tried this last time and it was too vague so I'll try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.