Edgar
Edgar

Reputation: 931

Accessing variable in other class

I've got 2 classes:

classVC: ViewController
classView: UIview

How can I access navAndStatusHeight from classVC in classView?

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

Upvotes: 0

Views: 51

Answers (3)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

If it is a child view,then declare the height as a property in parent view:

@property (nonatomic,strong) NSNumber *navAndStatusHeight;

then access this property from your child view. (I am considering child view as subview)

If you are not using segues,you can use NSUSerDefaults to store the int value and get it back anywhere you need.

To store an int:

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

To get it back:

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

Upvotes: 2

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

Get the topmost ViewController from this method

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

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

and then get the navigation controller and its frame from the topmost viewcontroller.

So your code

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

Upvotes: 0

carlos16196
carlos16196

Reputation: 716

in classView.h, declare your receiving variable as a property:

@property (nonatomic, assign) NSInteger navAndStatusHeight;

then in classVC.m, if using storyboard segue, go to prepareForSegue method and:

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

if using xibs, its pretty much the same thing but with your push navigation.

Upvotes: 0

Related Questions