数据在 ViewController 之间传递并被重置?

Data passed between ViewControllers and being Reset?

我只是想在 2 UIViewController 之间传递一个 NSInteger,但出于某种原因,数据不断重置。我所说的重置是指我执行以下操作:

- (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item
{
    PlayViewController *playScreen = [[PlayViewController alloc] init];
    playScreen.playerNumber = item;
    NSLog(@"%d", playScreen.playerNumber);  
}

然后 NSLog 会打印出所选对象的索引,但是一旦我转到 PlayViewController 并执行以下 switch 语句:

switch (self.playerNumber){
        case 0:
            theView.playerComment.text = @"You, again";
            playerScores[0]++;
            break;

        case 1:
            if (numberPressed % 2){
                theView.playerComment.text = @"Player 2's Turn";
                playerScores[0]++;
            }
            else {
                theView.playerComment.text = @"Player 1's Turn";
                playerScores[1]++;
            }
            break;
 and so on.... the code would always receive 0 as the value of playerNumber...

此外,在 PlayViewController.h 中,变量玩家编号是这样声明的:

@property (nonatomic) NSInteger playerNumber;

创建 PlayViewController 的 属性 并修改您的 pickerView didSelectItem: 委托。

@property (nonatomic, strong) PlayViewController *playScreen;

- (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item
{
    if (nil == self.playScreen)
    {
       self.playScreen = [[PlayViewController alloc] init];
    }

    playScreen.playerNumber = item;
    NSLog(@"%d", playScreen.playerNumber);  
}