目标C:从多个外部类访问同一类中的数据

时间:2012-02-27 18:22:52

标签: objective-c class

我是编程的小伙子,但我开始学习。我正在为iPhone写作,我现在仍然坚持这个问题。

我一直试图在stackowerflow上找到答案,但是很难将信息与我自己的项目联系起来。

正如标题所述,我正试图通过其他“外部”类从一个类访问数据。我已经习惯了Java,但我知道这不一样。

如何从类中的文本字段中保存字符串,以便稍后从其他类中检索它?

我这里有3个班级:

一个“模型” - 类来保存字符串数据 一个“view-controller” - 类将textfield-string保存在“model”类中 一个“ScoreBoard”类用于从“model”类中提取数据

首先是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

现在,class.h将字符串保存在“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

和最后一个ScoreBoard类,它访问名称字符串的模型: ScoreBoard.h:

#import <UIKit/UIKit.h>

@interface ScoreBoard : UIViewController

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

- (void)updateThatScoreBoard;

@end

和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

非常感谢任何投入!

1 个答案:

答案 0 :(得分:1)

实际上你应该把属性定义放到.h文件中,因为这是你要包含在其他类中的那个。

以下是此问题的版本:

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

到目前为止,我对您的样本进行了更改。您可以通过以下方式访问您的playerOne:

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

此外,如果没有必要,我不会在每个类中存储此模型的实例,只需将其传递给将要使用它的方法。

另一种可能性是在整个应用程序中只有一个模型实例。在这种情况下,您的模型可以按如下方式编程(您可以使用Singleton模式):

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

通过这种方式,您可以按如下方式访问模型实例:

someclass.m:

#include "Model.h"

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

anotherClass.m:

#include "Model.h"

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

您可以在Wikipedia

了解有关单身人士模式的更多信息