2015-04-15 139 views
0

我有2类:在ClassView访问变量在其他类

classVC: ViewController 
classView: UIview 

如何访问navAndStatusHeight从classVC?

int navAndStatusHeight = self.navigationController.navigationBar.frame.size.height 
         + [UIApplication sharedApplication].statusBarFrame.size.height; 
+0

是ClassView中classVC的子视图? –

+0

是的,这是一个子视图 – Edgar

+1

如果它是子视图,你可以访问父视图的所有属性 –

回答

2

如果它是一个子视图,然后声明高度为父视图属性:

@property (nonatomic,strong) NSNumber *navAndStatusHeight; 

然后从子视图访问此属性。 (我正在考虑子视图作为子视图)

如果您不使用segues,则可以使用NSUSerDefaults存储int值并将其返回到任何需要的位置。

要存储一个int:

[[NSUserDefaults standardUserDefaults] setInteger:navAndStatusHeight forKey:@"navHeight"]; 

把它找回来:

NSInteger height = [[NSUserDefaults standardUserDefaults] integerForKey:@"navHeight"]; 
+0

谢谢,这工作! – Edgar

0

从此方法

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 

while (topController.presentedViewController) { 
    topController = topController.presentedViewController; 
} 

获取最上面的ViewController,然后得到导航控制器和从最上面的ViewController其框架。

所以,你的代码

int navAndStatusHeight = CGRectGetHeight(topController.navigationController.navigationBar.frame) + 
         CGRectGetHeight([UIApplication sharedApplication].statusBarFrame); 
0

在classView.h,声明你接受变量作为一个属性:

@property (nonatomic, assign) NSInteger navAndStatusHeight; 

然后在classVC.m,如果用故事板SEGUE,去prepareForSegue方法和:

if ([[segue identifier] isEqualToString:@"theSegue"]) { 
     classView *cv = [segue destinationViewController]; 
     [cv setNavAndStatusHeight: yourValue]; 
} 

如果使用xibs,它几乎是相同的事情,但与您的推导航。