如何从对象中的对象获取变量(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//

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

游戏网:

#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 的初学者。

感谢建议,乔曼吉

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

所以你定义了一些应该交给下一个视图的变量?

由于您说您正在使用情节提要,因此您可以通过 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);

希望这有帮助..

你似乎缺少的是理解什么是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: 方法中进行设置,而不是使用局部变量。但这可能不是您问题的正确答案。您可能需要阅读局部变量与实例变量,以及变量和对象的生存期,以帮助您理解。

最新更新