objective-c - 从另一个 ViewController 设置 UIScrollView 的位置

标签 objective-c ios uiviewcontroller uiscrollview contentoffset

我试图通过使用 contentOffset 来设置 UIScrollView 的位置:

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

在关闭它之前,我从另一个 View Controller 调用此方法,并且一切都检查完毕。我正确地传递了参数,并且该方法被调用(用 NSLog 检查),但 ScrollView 没有移动...

有趣的是,当我从它所在的 View Controller 调用这个方法时,它工作得很好。只有当我从另一个 View Controller 调用它时,它才会停止工作。

调用方法如下,仅供引用:

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

内容偏移量是我事先设置的CGPoint。这里并不重要;此外,无论如何它都会正确通过。

最佳答案

试试这个,当你想改变时,你必须从其他viewcontroller发送通知..

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

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

关于objective-c - 从另一个 ViewController 设置 UIScrollView 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039488/

相关文章:

objective-c - 有时不调用 NSUbiquitousKeyValueStoreDidChangeExternallyNotification

ios - 终止 iOS 应用程序

ios - 如何使用 Swift 为 JSON 数据创建 JSON Codable 和 Decodable?

objective-c - 如何解决仍然为零的委托(delegate)

objective-c - 从 UITabBarController 中删除所有项目

ios - 使用 SKPhysics(碰撞)拖动 SKSpriteNode(跟随你的手指移动)

objective-c - NSMutableArray 作为实例变量总是 null

ios - 如何在 iOS 中正确设置背景图像

iphone - 在 iOS 中重新加载支持的界面方向

ios - 在哪里以编程方式构建 View 与使用 Nib