如何从对象中的对象获取变量(Objective-C)

时间:2012-08-11 08:37:16

标签: objective-c

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

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;

它只会给我一个错误!

顺便说一句,如果这是我们见过的最愚蠢的问题,或者有最明显的答案,我不要生我的气,我是客观的初学者。

Thanx in advice,JomanJi

2 个答案:

答案 0 :(得分: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);

希望这会有所帮助..

答案 1 :(得分:0)

您似乎缺少的是对instance variableproperty的理解,这是理解Objective-C中面向对象编程的基础。

考虑您的“Appdelaget.h”,其中定义了属性window。此属性属于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:方法中设置它而不是使用本地变量。但这可能不是您问题的正确答案。您可能希望阅读本地变量和实例变量,以及变量和对象的生命周期,以帮助您理解。