Alessandro
Alessandro

Reputation: 4100

Accessing UIScrollView from other ViewController

I have a viewcontroller, if I press a button, another viewController is loaded, but the first is not unloaded. I want to access a variable form the first view controller from the second, actually a UIScrollView, so that I can use it in this way:

scroll1.hidden = YES;

How can I do this? I tried importing the .h file, but still I cannot use the UIScrollView

EDIT:

NSArray* stack = [self.navigationController viewControllers];
NSInteger currentIndex = [stack indexOfObject:self];
ViewController* linkToA = (ViewController*)[stack objectAtIndex:currentIndex - 1];

[linkToA.scroll1.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[linkToA.scroll1 addSubview:linkToA.backgroundImage];

or

ViewController *linkToA = [[ViewController alloc] init];
linkToA = (ViewController*)self.presentingViewController;

[linkToA.scroll1.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[linkToA.scroll1 addSubview:linkToA.backgroundImage];

Upvotes: 0

Views: 388

Answers (1)

Ezeki
Ezeki

Reputation: 1519

just make sure first view controller does have a property of your scrollView in the header:

@property (nonatomic, strong) UIScrollView *scrollView;

if you don't have a latest xcode put @synthesize scrollView to the implementation of the first controller

lets say your first controller is A and the second is B

if B is a added as a modal then use this:

linkToA = (A*)B.presentingViewController;

if B is added to navigationControllerStack:

NSArray* stack = [B.navigationController viewControllers];
NSInteger currentIndex = [stack indexOfObject:B];
linkToA = (A*)[stack objectAtIndex:currentIndex - 1];

after you have a linkToA you have to do

linkToA.scrollView.hidden = YES;

hope this will help

Upvotes: 1

Related Questions