-3

我正在使用情节提要制作基本游戏。我有多个视图(当然),当重新打开视图时,它们会重置。所以,我创建了一个 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

4

2 回答 2

0

如果我正确理解您的问题,这基本上很容易解决。

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

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

快速示例...

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

因此,当您转到 viewTwo 时,您使用以下 segue 行:

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

这会将 MaxNr 的值移交给 viewTwo。

在 viewTwo 中,您显然必须声明并合成您的 maxNr。

viewTwo.h
....

@property (nonatomic) (int) maxNr;

viewTwo.m
....

@synthesize maxNr;

如果你想检查你是否做对了,你可以在 viewTwo didLoad 中放入以下行:

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

希望这可以帮助..

于 2012-08-11T10:16:47.620 回答
0

您似乎缺少对什么是instance variablea 或 a的理解property,这是理解 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:而不是使用局部变量。但这可能不是您问题的正确答案。您可能需要阅读本地变量与实例变量以及变量和对象的生命周期,以帮助您理解。

于 2012-08-11T10:31:47.033 回答