0

問題があります。いくつかのビュー コントローラーがあります。一方はPSACurrentGame、もう一方は と呼ばれPSAEnterScoresます。PSACurrentGameの親クラスですPSAEnterScoresPSACurrentGameからラベルを変更しようとしていPSAEnterScoresます。しかし、私はすでに親クラスのことでそれを理解しています。本当の問題は、正しいプロパティが変数にリンクされているにもかかわらず、変数のテキストが変更されないことです。メソッドの変更には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

この例をアプリケーションで使用して、View Controller 間でデータを転送しました。だから、それは役立つかもしれません:

于 2013-09-12T12:54:00.693 に答える
0

改善できる点は次の 2 つだと思います。

1) インターフェイス コントロールごとに 1 つ作成する代わりに、IBOutletCollection を使用します。

2) 共有データソースを使用し、その値が変更されたときに通知を受け取ります。次に、次のように、独自の ViewController 内でコントロールを変更できます。

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 に答える