访问其他 class 中的变量
Accessing variable in other class
我有 2 个 类:
classVC: ViewController
classView: UIview
如何从 classView 中的 classVC 访问 navAndStatusHeight
?
int navAndStatusHeight = self.navigationController.navigationBar.frame.size.height
+ [UIApplication sharedApplication].statusBarFrame.size.height;
通过此方法获取最顶层ViewController
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
然后从最上面viewcontroller.
获取导航控制器及其框架
所以你的代码
int navAndStatusHeight = CGRectGetHeight(topController.navigationController.navigationBar.frame) +
CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
在 classView.h 中,将您的接收变量声明为 属性:
@property (nonatomic, assign) NSInteger navAndStatusHeight;
然后在 classVC.m 中,如果使用故事板 segue,请转到 prepareForSegue 方法并:
if ([[segue identifier] isEqualToString:@"theSegue"]) {
classView *cv = [segue destinationViewController];
[cv setNavAndStatusHeight: yourValue];
}
如果使用 xibs,它几乎是一样的东西,但有你的推送导航。
如果是子视图,则在父视图中声明高度为属性:
@property (nonatomic,strong) NSNumber *navAndStatusHeight;
然后从您的子视图访问此 属性。 (我正在考虑子视图作为子视图)
如果你不使用 segues,你可以使用 NSUSerDefaults 来存储 int 值并在你需要的任何地方取回它。
要存储一个整数:
[[NSUserDefaults standardUserDefaults] setInteger:navAndStatusHeight forKey:@"navHeight"];
取回:
NSInteger height = [[NSUserDefaults standardUserDefaults] integerForKey:@"navHeight"];
我有 2 个 类:
classVC: ViewController
classView: UIview
如何从 classView 中的 classVC 访问 navAndStatusHeight
?
int navAndStatusHeight = self.navigationController.navigationBar.frame.size.height
+ [UIApplication sharedApplication].statusBarFrame.size.height;
通过此方法获取最顶层ViewController
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
然后从最上面viewcontroller.
获取导航控制器及其框架所以你的代码
int navAndStatusHeight = CGRectGetHeight(topController.navigationController.navigationBar.frame) +
CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
在 classView.h 中,将您的接收变量声明为 属性:
@property (nonatomic, assign) NSInteger navAndStatusHeight;
然后在 classVC.m 中,如果使用故事板 segue,请转到 prepareForSegue 方法并:
if ([[segue identifier] isEqualToString:@"theSegue"]) {
classView *cv = [segue destinationViewController];
[cv setNavAndStatusHeight: yourValue];
}
如果使用 xibs,它几乎是一样的东西,但有你的推送导航。
如果是子视图,则在父视图中声明高度为属性:
@property (nonatomic,strong) NSNumber *navAndStatusHeight;
然后从您的子视图访问此 属性。 (我正在考虑子视图作为子视图)
如果你不使用 segues,你可以使用 NSUSerDefaults 来存储 int 值并在你需要的任何地方取回它。
要存储一个整数:
[[NSUserDefaults standardUserDefaults] setInteger:navAndStatusHeight forKey:@"navHeight"];
取回:
NSInteger height = [[NSUserDefaults standardUserDefaults] integerForKey:@"navHeight"];