初始化属性

时间:2014-03-10 13:01:41

标签: iphone objective-c uiviewcontroller

所以我有一个类,我需要在第一次初始化后初始化一些属性一次。这些属性不需要在整个程序运行时重新初始化。我如何在iOS 7 SDK中执行此操作?我尝试使用initWithNibName,但它根本没有运行。与init相同。我应该使用什么方法?

这是我的代码

PSACurrentGame.h:


#import <UIKit/UIKit.h>
#import "phase10Scorer.h"

@interface PSACurrentGame : UIViewController

@property (strong, nonatomic) NSArray *playerNamesArray;
@property (nonatomic) BOOL moveNums;
@property (strong, nonatomic) phase10Scorer *scorer;
@property (strong, nonatomic) NSMutableArray *R1Scores;
@property (strong, nonatomic) NSMutableArray *R1Phases;
@property (strong, nonatomic) NSMutableArray *R2Scores;
@property (strong, nonatomic) NSMutableArray *R2Phases;
@property (strong, nonatomic) NSMutableArray *R3Scores;
@property (strong, nonatomic) NSMutableArray *R3Phases;
@property (strong, nonatomic) NSMutableArray *ToScores;
@property (strong, nonatomic) NSMutableArray *ToPhases;

@end

PSACurrentGame.m:

#import "PSACurrentGame.h"
#import "phase10Scorer.h"

@interface PSACurrentGame ()
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round1Scores;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round1Phases;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round2Scores;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round2Phases;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round3Scores;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *round3Phases;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *totalScores;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *totalPhases;


@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *playerNames;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *roundLabelsSecondSet;


@end

@implementation PSACurrentGame

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.scorer = [[phase10Scorer alloc] init];
        self.R1Scores = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
         self.R1Phases = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.R2Scores = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.R2Phases = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.R3Scores = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.R3Phases = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.ToScores = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.ToPhases = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];
        self.moveNums = true;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    int j = 0;
    for (UILabel *playerLabel in self.playerNames) {
        playerLabel.text = self.playerNamesArray[j];
        j++;
    }

    if (self.moveNums) {


    for (int i = 0; i < 5; i++) {
        self.R3Scores[i] = self.R2Scores[i];
        self.R2Scores[i] = self.R1Scores[i];
        self.R1Scores[i] = self.ToScores[i];

        self.R3Phases[i] = self.R2Phases[i];
        self.R2Phases[i] = self.R1Phases[i];
        self.R1Phases[i] = self.ToPhases[i];

        self.ToScores[i] = [NSNumber numberWithInt:([self.scorer getScores:(i + 1)])];
        self.ToPhases[i] = [NSNumber numberWithInt:([self.scorer getPhases:(i + 1)])];
    }
    }
    int i = 0;
    for (UILabel *tf in self.totalScores) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            pName.hidden = true;
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.ToScores[i];
        i++;
    }

    i = 0;

    for (UILabel *tf in self.totalPhases) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.ToPhases[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round1Scores) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R1Scores[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round1Phases) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R1Phases[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round2Scores) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R2Scores[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round2Phases) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R2Phases[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round3Scores) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R1Scores[i];
        i++;
    }

        i = 0;

    for (UILabel *tf in self.round3Phases) {
        UILabel *pName = self.playerNames[i];
        if ([pName.text isEqualToString:@""]) {
            tf.hidden = true;
            i++;
            continue;
        }
        tf.text = self.R1Phases[i];
        i++;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:1)

您应该使用awakeFromNib。

- (void) awakeFromNib {
     //Custom initialization
}

从XIB或故事板实例化视图控制器时,不会调用initWithNibName。

答案 1 :(得分:0)

嗯,还有initWithStyle:initWithCoder::)

您可以修改它们,但我认为更简单的解决方案是使用'延迟初始化'。 例如,在您的界面中,您的@property定义如下:

@interface YourClass
...
@property (nonatomic, strong, readonly) YourType* propertyName;
...
@end

请注意readonly属性。这意味着,此属性不会有可公开访问的setter(您提到过,这些属性不会被修改)。 然后在.m文件中,您可以定义一个类别:

@interface YourClass()
...
@property (nonatomic, strong) YourType* propertyName; //this will give us a private setter.
...
@end

然后你定义一个这样的getter:

@implementation
...
-(YourType*)propertyName
{
    if (nil == _propertyName)
    {
        //initialize _propertyName here. This code will run once.
    }
    return _propertyName;
}
...
@end

这样,属性将按需初始化。好处是可以保证它们会被初始化。糟糕的是,对这些方法的第一次调用需要更多时间。但还有另一个好处:对象创建更便宜。

P.S。此代码使用ARC。