2012-08-11 47 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