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!
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
model.m
So far my changes to your sample. You might access your playerOne by:
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:
model.m:
In this way you can access your model instance as follows:
someclass.m:
anotherClass.m:
You can read more on the singleton pattern at Wikipedia