【问题标题】:How to access a variable's value from one class, if the variable is defined in another? (Objective-C)如果变量在另一个类中定义,如何从一个类访问变量的值? (目标-C)
【发布时间】:2010-03-25 16:58:08
【问题描述】:

我有两对文件(.m 和 .h)。在一个接口部分,我定义了一个全局 BOOL 变量。我需要在另一个类中获得它的价值。我该怎么做? (我不能让一个类成为另一个类的子类)。

在一个文件中我有

@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

在另一个人的@implementation 我有

- (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];
}

}

【问题讨论】:

  • 请提供一些示例代码以使您的问题更清楚。
  • 特别是,怎么可能既是全局接口又是类接口呢?

标签: objective-c boolean


【解决方案1】:

如果你真的想要一个全局变量,你不希望它出现在任何一个类的接口中。您可以像在 C 中一样访问全局变量。使用:

extern BOOL myGlobal;

在您需要使用它的任何地方声明变量(或者在一个公共标头中可能是理想的)。然后在一个地方定义它:

BOOL myGlobal;

你应该准备好了。

如果您确实希望它成为一个类的实例变量,您可以像平常一样从另一个类获取该变量。

【讨论】:

  • 其实我对C也不是很常用。应该把extern BOOL myGlobal;主要吗?
  • 如果它是全局的,它不会“进入”任何东西,它是全局的——你可以将它与你拥有的任何其他全局变量一起声明——函数、变量等等。在尝试过多地参与 Objective-C 之前,您可能需要先阅读基本的 C 教程。
  • 好吧,我终于明白它是怎么做的了,但是我得到一个错误“_myGlobal”,引用自:myUIViewController1.o 中的_myGlobal$non_lazy_ptr _myUIViewController2.o 中的_myGlobal$non_lazy_ptr(也许你的意思是: _portrait$non_lazy_ptr) Symbol(s) not found myViewController2 是定义全局变量的类,myViewController1 是我需要它的值的类
  • @Mike,听起来你已经在两个地方都声明了它,但没有在任何地方定义它。
  • 所以我有一个文件 Global.h,它只包含 extern BOOL myGlobal;然后我有 myViewController1.h,在界面部分我定义它:@interface myViewController1 : UIViewController { BOOL Portrait; @end 然后在 myViewController1 的@implementation 中更改它的值。然后在 myViewController2 的@implementation 中,我读取了 BOOL 的值。但它仍然不起作用:(
猜你喜欢
  • 2011-10-22
  • 2018-04-09
  • 2015-02-28
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多