-3

絵コンテを使って基本的なゲームを作っています。私は(もちろん)複数の​​ビューを持っていますが、ビューを再度開くとリセットされます。そこで、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!!!!)//

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

4

2 に答える 2

0

あなたが見逃しているように見えるのは、またはが何であるinstance variableかを理解することpropertyであり、これは、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

この問題の解決策の1つは、タイプのインスタンス変数(またはプロパティ)をクラスに追加VariableControlし、ローカル変数を使用する代わりにメソッドAppDelegateで設定することです。application:didFinishLaunchingWithOptions:しかし、それはあなたの問題に対する正しい答えではないかもしれません。理解を助けるために、ローカル変数とインスタンス変数、および変数とオブジェクトの存続期間を確認することをお勧めします。

于 2012-08-11T10:31:47.033 に答える
0

あなたの質問を正しく理解していれば、これは基本的に簡単に解決できます..

次のビューに渡す変数をいくつか定義しましたか?

ストーリーボードを使用していると言うので、これらの値を segue コマンドで渡すことができます。

簡単な例...

viewOne で、MaxNr という変数を作成します。(別のクラスで宣言する必要はありません!!)

したがって、viewTwo に移動するときは、次のセグエ行を使用します。

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 に答える