0

我在 DifficultySelection 中创建了一个变量(难度),当我尝试从 CharacterSelection 访问这个变量时,结果 = 0,所以我猜测某处的封装存在问题。

难度选择.h:

@class CharacterSelection;
@interface DifficultySelection : UIViewController
{
    CharacterSelection *charPage;
    UIButton *normalDifficulty;
    int difficulty;

}

- (IBAction)normalDifficulty:(id)sender;

@property (strong) CharacterSelection *charPage;
@property (strong) DifficultySelection *diffPage;    
@property (strong, nonatomic) IBOutlet UIButton *normalDifficulty;
@property (nonatomic, readwrite) int difficulty;
@end

难度选择.m:

#import "DifficultySelection.h"
#import "CharacterSelection.h"

@interface DifficultySelection ()  
@end

@implementation DifficultySelection
@synthesize normalDifficulty, charPage, diffPage, difficulty;

- (IBAction)normalDifficulty:(id)sender {
    if (! self.charPage)
        self.charPage = [[CharacterSelection alloc] initWithNibName:nil bundle:nil];
    self.difficulty = 1;
    charPage.diffPage = self;
    [self.navigationController pushViewController:self.charPage animated:YES];

}
@end

字符选择.h:

#import "DifficultySelection.h"
@class MainGameDisplay;
@class DifficultySelection;

@interface CharacterSelection : UIViewController <UIScrollViewDelegate> {
    MainGameDisplay *openGame;
    DifficultySelection *diffPage;
}

@property (nonatomic, readwrite) CharacterSelection *charPage;    
@property (strong) DifficultySelection *diffPage;
@property (nonatomic, readwrite) int difficulty;
@end

字符选择.m:

#import "CharacterSelection.h"
#import "MainGameDisplay.h"

@interface CharacterSelection ()
@end

@implementation CharacterSelection
@synthesize diffPage, charPage;

- (void)viewDidLoad
{
    self.difficulty = charPage.diffPage.difficulty;
    printf("Diff:%i %i", self.difficulty, diffPage.difficulty);
    //Both of these output 0, encapsulation problem.
}
@end
4

0 回答 0