0

我有一个问题,我有几个视图控制器。一个叫PSACurrentGame,另一个叫PSAEnterScoresPSACurrentGame是 的父类PSAEnterScores。我正在尝试将标签PSACurrentGamePSAEnterScores. 但我已经用父类的东西弄清楚了。真正的问题是,虽然我有与变量链接的正确属性,但变量的文本不会改变。我确认 lanbel 响应viewDidLoad方法的更改,但它不会响应它在子类中的更改。有谁知道为什么?

下面的代码:

PSACurrentGame.h

@interface PSACurrentGame : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *R1P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P1Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P1Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP1Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP1Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P2Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P2Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP2Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP2Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P3Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P3Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP3Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP3Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P4Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P4Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP4Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP4Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P5Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P5Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP5Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP5Phase;
@property (strong, nonatomic) IBOutlet UILabel *R1P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R1P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *R2P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R2P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *R3P6Score;
@property (strong, nonatomic) IBOutlet UILabel *R3P6Phase;
@property (strong, nonatomic) IBOutlet UILabel *ToP6Score;
@property (strong, nonatomic) IBOutlet UILabel *ToP6Phase;
@property (strong, nonatomic) IBOutlet UILabel *player1;
@property (strong, nonatomic) IBOutlet UILabel *player2;
@property (strong, nonatomic) IBOutlet UILabel *player3;
@property (strong, nonatomic) IBOutlet UILabel *player4;
@property (strong, nonatomic) IBOutlet UILabel *player5;
@property (strong, nonatomic) IBOutlet UILabel *player6;
@property (strong, nonatomic) IBOutlet UILabel *R1List2;
@property (strong, nonatomic) IBOutlet UILabel *R2List2;
@property (strong, nonatomic) IBOutlet UILabel *R3List2;
@property (strong, nonatomic) IBOutlet UILabel *ToList2;
- (void) updateScores;


@end

PSACurrentGame.m

@interface PSACurrentGame ()



@end

@implementation PSACurrentGame

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view
    self.ToP1Score = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];
    [self.ToP1Score setTextColor:[UIColor redColor]];
    [self.ToP1Score setFont:[UIFont fontWithName:@"Arial" size:30]];
    PSAGlobal *global = [PSAGlobal getInstance];

    self.player1.text = global.player1Name;
    self.player2.text = global.player2Name;
    self.player3.text = global.player3Name;
    self.player4.text = global.player4Name;
    self.player5.text = global.player5Name;
    self.player6.text = global.player6Name;

    if ([self.player3.text isEqual: @""]) {
        self.player3.hidden = YES;
        self.R1P3Phase.hidden = YES;
        self.R1P3Score.hidden=YES;
        self.R2P3Phase.hidden = YES;
        self.R2P3Score.hidden=YES;
        self.R3P3Phase.hidden = YES;
        self.R3P3Score.hidden=YES;
        self.ToP3Phase.hidden = YES;
        self.ToP3Score.hidden=YES;
    }

    if ([self.player4.text isEqual: @""]) {
        self.player4.hidden = YES;
        self.R1P4Phase.hidden = YES;
        self.R1P4Score.hidden=YES;
        self.R2P4Phase.hidden = YES;
        self.R2P4Score.hidden=YES;
        self.R3P4Phase.hidden = YES;
        self.R3P4Score.hidden=YES;
        self.ToP4Phase.hidden = YES;
        self.ToP4Score.hidden=YES;
    }
    if ([self.player5.text isEqual: @""]) {
        self.player5.hidden = YES;
        self.R1P5Phase.hidden = YES;
        self.R1P5Score.hidden=YES;
        self.R2P5Phase.hidden = YES;
        self.R2P5Score.hidden=YES;
        self.R3P5Phase.hidden = YES;
        self.R3P5Score.hidden=YES;
        self.ToP5Phase.hidden = YES;
        self.ToP5Score.hidden=YES;
    }

    if ([self.player6.text isEqual: @""]) {
        self.player6.hidden = YES;
        self.R1P6Phase.hidden = YES;
        self.R1P6Score.hidden=YES;
        self.R2P6Phase.hidden = YES;
        self.R2P6Score.hidden=YES;
        self.R3P6Phase.hidden = YES;
        self.R3P6Score.hidden=YES;
        self.ToP6Phase.hidden = YES;
        self.ToP6Score.hidden=YES;
    }

    if ([self.player4.text isEqual: @""] && [self.player5.text isEqual:@""] && [self.player6.text isEqual:@""]) {
        self.R1List2.hidden = YES;
        self.R2List2.hidden = YES;
        self.R3List2.hidden = YES;
        self.ToList2.hidden = YES;
    }


}

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

PSAEnterScores.h

@interface PSAEnterScores : PSACurrentGame

@property (weak, nonatomic) IBOutlet UITextField *player1Txt;
@property (weak, nonatomic) IBOutlet UITextField *player2Txt;
@property (weak, nonatomic) IBOutlet UITextField *player3Txt;
@property (weak, nonatomic) IBOutlet UITextField *player4Txt;
@property (weak, nonatomic) IBOutlet UITextField *player5Txt;
@property (weak, nonatomic) IBOutlet UITextField *player6Txt;
@property (weak, nonatomic) IBOutlet UILabel *player1Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player2Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player3Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player4Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player5Lbl;
@property (weak, nonatomic) IBOutlet UILabel *player6Lbl;

@end

PSAEnterScores.m

@interface PSAEnterScores ()

- (IBAction)ok:(id)sender;

@end

@implementation PSAEnterScores

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    PSAGlobal *global = [PSAGlobal getInstance];
    self.player1Lbl.text = global.player1Name;
    self.player2Lbl.text = global.player2Name;
    self.player3Lbl.text = global.player3Name;
    self.player4Lbl.text = global.player4Name;
    self.player5Lbl.text = global.player5Name;
    self.player6Lbl.text = global.player6Name;

    if ([self.player3Lbl.text isEqual:@""]) {
        self.player3Lbl.hidden = YES;
        self.player3Txt.hidden = YES;
    }

    if ([self.player4Lbl.text isEqual:@""]) {
        self.player4Lbl.hidden = YES;
        self.player4Txt.hidden = YES;
    }

    if ([self.player5Lbl.text isEqual:@""]) {
        self.player5Lbl.hidden = YES;
        self.player5Txt.hidden = YES;
    }

    if ([self.player6Lbl.text isEqual:@""]) {
        self.player6Lbl.hidden = YES;
        self.player6Txt.hidden = YES;
    }



}

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


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;

}

- (IBAction)ok:(id)sender {
    [self.ToP1Score setText:self.player1Txt.text];

    self.R3P1Score.text = self.R2P1Score.text;
    self.R3P1Phase.text = self.R2P1Phase.text;
    self.R3P2Score.text = self.R2P2Score.text;
    self.R3P2Phase.text = self.R2P2Phase.text;
    self.R3P3Score.text = self.R2P3Score.text;
    self.R3P3Phase.text = self.R2P3Phase.text;
    self.R3P4Score.text = self.R2P4Score.text;
    self.R3P4Phase.text = self.R2P4Phase.text;
    self.R3P5Score.text = self.R2P5Score.text;
    self.R3P5Phase.text = self.R2P5Phase.text;
    self.R3P6Score.text = self.R2P6Score.text;
    self.R3P6Phase.text = self.R2P6Phase.text;
    //Round 1 -> Round 2
    self.R2P1Score.text = self.R1P1Score.text;
    self.R2P1Phase.text = self.R1P1Phase.text;
    self.R2P2Score.text = self.R1P2Score.text;
    self.R2P2Phase.text = self.R1P2Phase.text;
    self.R2P3Score.text = self.R1P3Score.text;
    self.R2P3Phase.text = self.R1P3Phase.text;
    self.R2P4Score.text = self.R1P4Score.text;
    self.R2P4Phase.text = self.R1P4Phase.text;
    self.R2P5Score.text = self.R1P5Score.text;
    self.R2P5Phase.text = self.R1P5Phase.text;
    self.R2P6Score.text = self.R1P6Score.text;
    self.R2P6Phase.text = self.R1P6Phase.text;
    //Total -> Round 1
    self.R1P1Score.text = self.ToP1Score.text;
    self.R1P1Phase.text = self.ToP1Phase.text;
    self.R1P2Score.text = self.ToP2Score.text;
    self.R1P2Phase.text = self.ToP2Phase.text;
    self.R1P3Score.text = self.ToP3Score.text;
    self.R1P3Phase.text = self.ToP3Phase.text;
    self.R1P4Score.text = self.ToP4Score.text;
    self.R1P4Phase.text = self.ToP4Phase.text;
    self.R1P5Score.text = self.ToP5Score.text;
    self.R1P5Phase.text = self.ToP5Phase.text;
    self.R1P6Score.text = self.ToP6Score.text;
    self.R1P6Phase.text = self.ToP6Phase.text;
    //Setting Total's score
    //Setting Player 1's Score // This section is where the ToP1Score should be changed, but it isn't!
    NSInteger curScore = [self.ToP1Score.text integerValue];
    NSInteger curPhase = [self.ToP1Phase.text integerValue];
    self.ToP1Score.text = [NSString stringWithFormat:@"%d", (curScore += [self.player1Txt.text integerValue])];
    if ([self.player1Txt.text integerValue] < 50) {
        self.ToP1Phase.text = [NSString stringWithFormat:@"%d", (curPhase += 1)];
    }


   }

- (void) updateScores {

}

@end

如果您需要更多内容,请发表评论,我会添加。

编辑:我改变了顶部的文字,但我也会把它放在这里。标签现在响应其自己类中的更改,但现在是子类中的更改。子类是否只有只读权限才能访问父类变量?

4

2 回答 2

0

我认为你有两点可以改进:

1)使用 IBOutletCollection 而不是为每个界面控件创建一个;

2) 使用共享数据源并在其值更改时得到通知。然后你可以在你的控件中进行更改,在它们自己的 ViewControllers 中,像这样:

self.dataStore addObserver:self forKeyPath:@"gameScore" options:0 context:nil];

//...

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {

    if ([keyPath isEqual:@"gameScore"]) {
        //update your IBOutletCollections...
    }
}

参考 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOBasics.html#//apple_ref/doc/uid/20002252-BAJEAIEE

于 2013-09-10T04:57:32.897 回答
0

我在我的应用程序中使用了这个示例来在视图控制器之间传输数据。因此,它可能会有所帮助:http: //iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-and-other-objects-link-fixed.html

于 2013-09-12T12:54:00.693 回答