views:

84

answers:

1

I have two pairs (.m and .h) of files. In one's interface section, I've defined a global BOOL variable. I need to get it's value in another class. How can I do it? (I can't make one class a subclass of another).

In one file I have

@interface TabBarRotation : UITabBarController {
    BOOL portrait;
}

@end



@implementation TabBarRotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

- (void):(UIInterfaceOrientation) interfaceOrientation  { 
if (interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    portrait=YES;
} 
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    portrait=NO;
} 
}

@end

And in another's @implementation I have

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(portrait==YES) {
    CalcPortraitViewController *CalcPortraitController;
    CalcPortraitController = [[CalcPortraitViewController alloc] 
                 initWithNibName:@"CalcPortraitView" bundle:nil];
    CalcPortraitController.title=@"Калькулятор";
    CalcPortraitController.hidesBottomBarWhenPushed=YES;    
    [self.navigationController pushViewController:CalcPortraitController
                                     animated:NO];  
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
else if (portrait==NO) {
    CalcLandscapeViewController *CalcLandscapeController;
    CalcLandscapeController = [[CalcLandscapeViewController alloc] 
                              initWithNibName:@"CalcLandscapeView" bundle:nil];
    CalcLandscapeController.title=@"Калькулятор";
    CalcLandscapeController.hidesBottomBarWhenPushed=YES;    
    [self.navigationController pushViewController:CalcLandscapeController
                                         animated:NO];  
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

}
A: 

If you really want a global variable, you don't want it to be in the interface of either class. You can access global variables just like you would in C. Use:

extern BOOL myGlobal;

to declare the variable anywhere you need to use it (or in a common header might be ideal). Then define it in exactly one place:

BOOL myGlobal;

And you should be set.

If you do want it to be an instance variable of a class, you can make an accessor like you normally would to get at that variable from another class.

Carl Norum
Actually, I'm nor very common with C. Should I put extern BOOL myGlobal;In main?
Knodel
If it's a global it doesn't go "in" anything, it's global - you declare it alongside whatever other globals you have - functions, variables, whatever. You might want to go through a basic C tutorial before trying to get too involved with Objective-C.
Carl Norum
Ok, I've finally understood how it's done, but I get an error "_myGlobal", referenced from:_myGlobal$non_lazy_ptr in myUIViewController1.o _myGlobal$non_lazy_ptr in myUIViewController2.o(maybe you meant:_portrait$non_lazy_ptr)Symbol(s) not foundmyViewController2 is the class, where the global variable was defines and myViewController1 is the class where I needed it's value
Knodel
@Mike, sounds like you've declared it in both places but not defined it anywhere.
Carl Norum
So I have a file Global.h, which contains only extern BOOL myGlobal;Then I have myViewController1.h, where in the interface section I define it:@interface myViewController1 : UIViewController {BOOL portrait;@endThen in the @implementation of myViewController1 I change its value.Then in @implementation of myViewController2 I read the BOOL's value.But it still doesn't work :(
Knodel
@Mike - if it's a global it doesn't go in an `@interface` section. That's what global *means*. What you're defining is an instance variable.
Carl Norum