Question

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.

Was it helpful?

Solution

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];
 }

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top