Gergely Kovacs
Gergely Kovacs

Reputation: 1051

Setting UIScrollView's Position from another ViewController

I am trying to set the position of a UIScrollView by using contentOffset as such:

- (void) navigateToTableViewPosition:(CGPoint)contentOffset {
    NSLog(@"Position set method gets called...");
    NSLog(@"%@", NSStringFromCGPoint(contentOffset));
    [mainScrollView setContentOffset:contentOffset animated:YES];
}

I call this method from another view controller before I dismiss it, and everything checks out. I pass the argument correctly, and the method gets called (checked it with NSLog), but the scroll view does not move...

What is funny is that when I call this method from the view controller, in which it is located, it works fine. Only when I call it from another view controller, it stops working.

Just for future reference, here is the calling method:

MainViewController *mainView = [[MainViewController alloc] init];
[mainView navigateToTableViewPosition:contentOffset];

Content offset is a CGPoint I set beforehand. It doesn't matter here; besides, it gets passed correctly anyways.

Upvotes: 2

Views: 561

Answers (3)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Use delegate for backward messaging in view controllers.

Refer Basic Delegate Example link for more reference.

Your are making new instance of viewcontroller which will call method but will have no effect.

Upvotes: 0

Kalpesh
Kalpesh

Reputation: 5334

Try this, You have to send notification from other viewcontroller when you want to change ..

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changepostion"        object:NSStringFromCGPoint(CGPointMake(contentOffset.x, contentOffset.y))];

in mainviewcontroller

  -(void)viewDidLoad

  {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
         [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(navigateToTableViewPosition:) name:@"changepostion" object:nil];

  }

- (void) navigateToTableViewPosition:(NSNotification *)notification
  {
        contentOffset =CGPointFromString([notification object]);

   NSLog(@"Position set method gets called...");
   NSLog(@"%@", NSStringFromCGPoint(contentOffset));
    [mainScrollView setContentOffset:contentOffset animated:YES];
 }

Upvotes: 2

dasdom
dasdom

Reputation: 14063

You can't set the properties of a view which is not visible. If you are using iOS5+ you can implement the offset setting in the completion in the view dismiss completion block.

Upvotes: 1

Related Questions