我正在使用情节提要制作基本游戏。我有多个视图(当然),当重新打开视图时,它们会重置。所以,我创建了一个 appdelaget 应该关闭对象的类。现在,我将 appdelaget 导入到所有需要在重新加载视图时传递变量/不重置的视图。现在,有谁知道我如何从另一个对象中创建的对象中获取变量。无论如何,这很难解释,以下是重要的类:
变量控制.h:
#import <Foundation/Foundation.h>
@interface VariableControl : NSObject
@property (nonatomic) int maxNr;
@property (nonatomic) int nrSet;
@property (nonatomic) int guessNr;
变量控制.m:
#import "VariableControl.h"
@implementation VariableControl
@synthesize maxNr;
@synthesize guessNr;
@synthesize nrSet;
@end
这是一个简单的类,它将保存将通过视图传递的变量。
Appdelaget.h:
#import <UIKit/UIKit.h>
#import "VariableControl.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
Appdelaget.m:
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
VariableControl *VarControll = [[VariableControl alloc] init];
VarControll.maxNr = 100;
VarControll.nrSet = arc4random() %VarControll.maxNr;
// Override point for customization after application launch.
return YES;
}
//More methods are not listed becouse they're non-touched//
游戏.h:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface Game : UIViewController
//User interaction and labels
@property (strong, nonatomic) IBOutlet UILabel *theTitle;
@property (strong, nonatomic) IBOutlet UITextField *theInput;
@property (strong, nonatomic) IBOutlet UILabel *theMessage;
@property (strong, nonatomic) IBOutlet UINavigationItem *theTabTitle;
@property (strong, nonatomic) IBOutlet UIButton *theGuessButton;
@property (strong, nonatomic) IBOutlet UIButton *theNewGameButton;
//Variables
@property (nonatomic) int number;
@property (nonatomic) int guess;
@property (nonatomic) int nrOfGuess;
@property (nonatomic) int maxNr;
@property (nonatomic, retain) NSString *guessString;
//Actions
- (IBAction)guess:(id)sender;
- (IBAction)newGame:(id)sender;
@end
//Have some non-neded outlets becouse I tried to fix SIGABRT error, and didn't remove
them(btw, I have solved sigabrt!!!!)//
游戏.m:
#import "Game.h"
@implementation Game
@synthesize theTitle;
@synthesize theInput;
@synthesize theMessage;
@synthesize theTabTitle;
@synthesize theGuessButton;
@synthesize theNewGameButton;
@synthesize number;
@synthesize nrOfGuess;
@synthesize guess;
@synthesize guessString;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
AppDelegate *StartUp = [[AppDelegate alloc] init];
return YES;
}
- (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.
//When the user taps somwhere away from the keyboard, it disapears
[theInput resignFirstResponder];
[theTabTitle setTitle:@"Game"];
[theNewGameButton setTitle:@"New Game" forState:UIControlStateNormal];
[theGuessButton setTitle:@"Guess" forState:UIControlStateNormal];
//set a random number and clear variables
nrOfGuess = 0;
guess = 0;
}
- (void)viewDidUnload
{
[self setTheTitle:nil];
[self setTheInput:nil];
[self setTheMessage:nil];
[self setTheTabTitle:nil];
[self setTheGuessButton:nil];
[self setTheNewGameButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[theInput resignFirstResponder];
//If the user touches outside the keyboard, it will disapear
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)guess:(id)sender {
guess = [[theInput text] intValue];
if (guess == number) {
guessString = [NSString stringWithFormat:@"Corret! You guessed %i times!", nrOfGuess];
[theMessage setText: guessString];
number = arc4random() %101;
nrOfGuess = 0;
guess = 0;
}
else {
if (guess < number) {
[theMessage setText:@"Sorry! Guessed too low!"];
nrOfGuess = nrOfGuess + 1;
[theInput setText:@""];
}
else {
[theMessage setText:@"Sorry! Guessed too high!"];
nrOfGuess = nrOfGuess + 1;
[theInput setText:@""];
}
}
}
- (IBAction)newGame:(id)sender {
//set a random number and clear variables
number = arc4random() %101;
nrOfGuess = 0;
guess = 0;
}
@end
现在,问题是;在game.m中,我怎样才能在“VarControll”中获得变量maxNr 这是appdelaget.m中创建的类VariableControll的对象。我不可能
number = StartUp.VarControll.maxNr;
它只会给我一个错误!
顺便说一句,如果这是您见过的最愚蠢的问题,或者有最明显的答案,请不要生我的气,我是目标 c 的初学者。
谢谢你的建议,JomanJi