2015-04-15 141 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,它幾乎是相同的事情,但與您的推導航。