我在DifficultySelection中创建了一个变量(难度),当我尝试从CharacterSelection到达这个变量时,结果= 0,所以我猜测某个地方有封装问题。
DifficultySelection.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
DifficultySelection.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
CharacterSelection.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
CharacterSelection.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