2012-08-11 33 views
-3

我正在使用故事板制作基本游戏。我有多个视图(当然),当重新打开视图时,它们重置。所以,我创建了一个appdelaget应该关闭对象的类。 现在,我将appdelaget导入所有需要传递变量的视图/不重置视图重新加载时。现在, 没有人知道我是如何从另一个对象内创建的对象获取变量。不管怎么说,这是很难解释,这里是重要的类:如何从对象中获取变量(Objective-C)

VariableControll.h:

#import <Foundation/Foundation.h> 

@interface VariableControl : NSObject 

@property (nonatomic) int maxNr; 
@property (nonatomic) int nrSet; 
@property (nonatomic) int guessNr; 

VariableControll.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// 

Game.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!!!!)// 

Game.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

回答

0

这基本上是很容易解决,如果我明白你的问题正确的,是..

所以你定义一些变量应该交给未来的看法?

既然你说你正在使用故事板,你可以通过你的segue命令来传递这些值。

快速示例...

在viewOne中,您将创建一个名为MaxNr的变量。 (无需声明他们独立的类!)

所以,当你去viewTwo,您使用以下SEGUE行:

if ([segue.identifier isEqualToString:@"gotoViewTwo"]) 
{ 
UIViewController *viewTwo = [segue destinationViewController]; 
viewTwo.maxNr=self.maxNr; 
} 

这将移交MaxNr到viewTwo值。

鉴于你显然必须声明和综合你的maxNr。

viewTwo.h 
.... 

@property (nonatomic) (int) maxNr; 

viewTwo.m 
.... 

@synthesize maxNr; 

如果要检查你是否做的对,你把viewTwo didLoad下面一行:

NSLog(@"My viewTwo maxNr value is %u",maxNr); 

希望这有助于..

+0

就是这样,Thanx! – JomanJi 2012-08-11 14:15:42

+0

你能接受这个答案吗? – 2012-08-12 07:44:48

+0

大声笑,我以为我确实.....对不起! – JomanJi 2012-08-14 18:30:31

0

你似乎什么是失踪是了解什么是instance variableproperty是什么,这对理解Objective-C中的面向对象编程至关重要。

考虑这样的属性window定义你的“Appdelaget.h”。此属性属于AppDelegate的实例。所以,你可以写:

AppDelegate *StartUp = [[AppDelegate alloc] init]; // create your instance of AppDelegate 
NSWindow *theWindow = Startup.window; // obtain the value of the window property 

但是你的变量VarControll不是一个实例变量(即属于)类,但它是一个局部变量(即属于)的方法 - 它诞生时的方法被调用并在方法存在时被销毁。这就是为什么你不能写:

Startup.VarControll; // wrong, Startup has no property VarControll 
Startup->VarControll; // also wrong, Startup has no instance variable VarControll 

一个解决问题的方法是VariableControl类型的实例变量(或属性)添加到您的AppDelegate类,并设置在你application:didFinishLaunchingWithOptions:方法,而不是使用一个局部变量。但这可能不是你问题的正确答案。您可能需要阅读本地变量和实例变量,以及变量和对象的生命周期以帮助您理解。

+0

是的,我认为我做错了是我使用局部变量而不是实例变量 – JomanJi 2012-08-11 13:58:17