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

  • SEARCH
  • Home
  • 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 7591315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:37:18+00:00 2026-05-30T20:37:18+00:00

I’m a noob at programming, but I’m starting to learn. I’m writing for iPhone,

  • 0

I’m a noob at programming, but I’m starting to learn. I’m writing for iPhone, and I’m now stuck at with this problem.

I have been trying to find an answer here on stackowerflow, but have had a hard time relating the information to my own project.

As the title states, I’m trying to access data from one class through other “external” classes. I’m used to Java, but i know that this doesn’t work the same way.

How do I save the string from a textfield in a class, only to later retrieve it from other classes?

I here present 3 classes:

One “model”-class to save the string-data
One “view-controller”-class to save the textfield-string in the “model”-class
One “ScoreBoard”-class to extract the data from the “model”-class

first the model.h:

#import <Foundation/Foundation.h>

@interface Model : NSObject

- (void)setPlayerOneName:(NSString *)tfString;
- (NSString *)getPlayerOne;

@end

model.m:

#import "Model.h"

@interface Model()
@property (strong) NSMutableString *playerOne;
@end

@implementation Model
@synthesize playerOne = _playerOne;

- (void)setPlayerOneName:(NSString *)tfString{
    [self.playerOne initWithCapacity:20];
    [self.playerOne setString:tfString];
}

- (NSString *)getPlayerOne{
    NSString *returnString = self.playerOne;
    return returnString;
}

@end

now, the class.h to save the string in the “Model”-class:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

.m:

#import "ViewController.h"
#import "Model.h"
#import "ScoreBoard.h"

@interface ViewController()

@property (strong) Model *model;

@end

@implementation Whist_CalculatorViewController

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self.model setPlayerOneName:textField.text]; 
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"saveAndStart"]){
        NSLog(@"prepareForSegue: %@", segue.identifier);

        [segue.destinationViewController updateThatScoreBoard];
    }
}

@end

and the last ScoreBoard class, wich access the model for the name-string:
ScoreBoard.h:

#import <UIKit/UIKit.h>

@interface ScoreBoard : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *player1Label;

- (void)updateThatScoreBoard;

@end

and ScoreBoard.m:

#import "ScoreBoard.h"
#import "Model.h"

@interface ScoreBoard()

@property (strong) Model *model;

@end

@implementation ScoreBoard

@synthesize player1Label = _player1Label;
@synthesize model = _model;

- (void)updateThatScoreBoard{
    [self.player1Label setText:[self.model getPlayerOne]];
}

- (void)viewDidLoad{
    [self updateThatScoreBoard];
}

- (void)viewDidUnload {
    [self setPlayer1Label:nil];
    [super viewDidUnload];
}
@end

Any inputs is much appreciated!

  • 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-05-30T20:37:20+00:00Added an answer on May 30, 2026 at 8:37 pm

    Actually you should put the property definition into your .h file, since this is the one you are going to include in other classes.

    Here is my version to this problem:

    model.h

    @interface Model : NSObject {
    @private
        NSString *playerOne;
    }
        // It is readonly, since you want this class use merely as a DTO
        @property (readonly) NSString playerOne;
    
        // Just give a static method that creates the DTO with some value
        // to keep the actual Model object immutable!
        +(id) createModelWithPlayer: (NSString*)p1;
    @end
    

    model.m

    #import "Model.h"
    
    @implementation Model
    @synthesize playerOne;
    
        #pragma mark - private accessors
        -(void) setPlayerOne:(NSString*)pO {
           playerOne = pO;
        }
        #pragma mark -
    
        +(id) createModelWithPlayer: (NSString*)p1 {
            Model *m = [[Model alloc] init];
    
            [m setPlayerOne: p1];
            return m;
        }
    @end
    

    So far my changes to your sample. You might access your playerOne by:

    Model *m = [Model createModelWithPlayer:@"Rookey"];
    ...
    NSLog(@"Player one: %@", m.playerOne);
    

    Further more, I won’t store the instance of this Model in every class if not really necessary, just pass it for the methods where it will be used.

    Another possibility is to have only one instance of the model across the whole application. In this case your model could be programmed as follows (you might use the Singleton pattern):

    model.h:

    @interface Model : NSObject
        // It is readonly, since you want this class use merely as a DTO
        @property (assign) NSString playerOne;
    
        // Just give a static method that creates the DTO with some value
        // to keep the actual Model object immutable!
        +(id) instance;
    @end
    

    model.m:

    #import "Model.h"
    
    @implementation Model
    @synthesize playerOne;
    
        #pragma mark - private accessors
        -(void) setPlayerOne:(NSString*)pO {
           playerOne = pO;
        }
        #pragma mark -
    
        static Model *oneAndOnlyInstance;
        +(id) instance {
            if ( oneAndOnlyInstance == nil )
                oneAndOnlyInstance = [[Model alloc] init];
    
            return oneAndOnlyInstance;
        }
    @end
    

    In this way you can access your model instance as follows:

    someclass.m:

    #include "Model.h"
    
    //...
    [Model instance].playerOne = @"Rookey";
    //...
    

    anotherClass.m:

    #include "Model.h"
    
    //...
    NSLog(@"player one: %@", [M instance].playerOne );
    //...
    

    You can read more on the singleton pattern at Wikipedia

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.