在 UITabBarController 中的 UIViewController 之间共享 UIView

发布于 2024-08-26 05:01:09 字数 2019 浏览 3 评论 0原文

我有一个 UIScrollView,其中包含用户可以滚动浏览的图像库。此视图需要在 UITabBarController 内的三个单独的 UIViewController 中的每一个上可见。现在,我在 UITabBarController 子类中有三个独立的 UIScrollView 实例,并且控制器管理保持三个同步(当用户滚动他们可以看到的一个时,以编程方式滚动其他两个以匹配等),这并不理想。

我想知道是否有一种方法可以仅使用 UIScrollView 的一个实例,但让它仅显示在用户当前正在与之交互的 UIViewController 中。这将完全消除所有同步代码。这基本上是我现在在 UITabBarController 中的内容(这是当前管理所有这些的地方):

@interface ScrollerTabBarController : UITabBarController {
  FirstViewController *firstView;
  SecondViewController *secondView;
  ThirdViewController *thirdView;

  UIScrollView *scrollerOne;
  UIScrollView *scrollerTwo;
  UIScrollView *scrollerThree;
}
@property (nonatomic,retain) IBOutlet FirstViewController *firstView;
@property (nonatomic,retain) IBOutlet SecondViewController *secondView;
@property (nonatomic,retain) IBOutlet ThirdViewController *thirdView;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerOne;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerTwo;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerThree;

@end

@implementation ScrollerTabBarController

- (void)layoutScroller:(UIScrollView *)scroller {}
- (void)scrollToMatch:(UIScrollView *)scroller {}

- (void)viewDidLoad {
  [self layoutScroller:scrollerOne];
  [self layoutScroller:scrollerTwo];
  [self layoutScroller:scrollerThree];

  [scrollerOne setDelegate:self];
  [scrollerTwo setDelegate:self];
  [scrollerThree setDelegate:self];

  [firstView setGallery:scrollerOne];
  [secondView setGallery:scrollerTwo];
  [thirdView setGallery:scrollerThree];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  [self scrollToMatch:scrollView];
}

@end

当用户滚动实例之一时,UITabBarController 会收到通知(作为滚动视图的委托),然后调用诸如 scrollToMatch 之类的方法: 将其他两个与用户的选择同步。

是否可以使用 IBOutlet 上的多对一关系或类似的方式来将其范围缩小到一个实例,这样我就不必管理三个滚动视图?我尝试保留单个实例,并使用 UITabBarControllerDelegate 方法将指针从一个视图移动到下一个视图(在当前视图上调用 setGallery:nil ,在下一个视图上调用 setGallery:scrollerOne )时间改变),但滚动条从未移动到其他选项卡。

提前致谢!

I have a UIScrollView that houses a gallery of images the user can scroll through. This view needs to be visible on each of three separate UIViewControllers that are housed within a UITabBarController. Right now, I have three separate UIScrollView instances in the UITabBarController subclass, and the controller manages keeping the three synchronized (when a user scrolls the one they can see, programmatically scrolling the other two to match, etc.), which is not ideal.

I would like to know if there is a way to work with only ONE instance of the UIScrollView, but have it show up only in the UIViewController that the user is currently interacting with. This would completely eliminate all the synchronization code. Here is basically what I have now in the UITabBarController (which is where all this is currently managed):

@interface ScrollerTabBarController : UITabBarController {
  FirstViewController *firstView;
  SecondViewController *secondView;
  ThirdViewController *thirdView;

  UIScrollView *scrollerOne;
  UIScrollView *scrollerTwo;
  UIScrollView *scrollerThree;
}
@property (nonatomic,retain) IBOutlet FirstViewController *firstView;
@property (nonatomic,retain) IBOutlet SecondViewController *secondView;
@property (nonatomic,retain) IBOutlet ThirdViewController *thirdView;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerOne;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerTwo;
@property (nonatomic,retain) IBOutlet UIScrollView *scrollerThree;

@end

@implementation ScrollerTabBarController

- (void)layoutScroller:(UIScrollView *)scroller {}
- (void)scrollToMatch:(UIScrollView *)scroller {}

- (void)viewDidLoad {
  [self layoutScroller:scrollerOne];
  [self layoutScroller:scrollerTwo];
  [self layoutScroller:scrollerThree];

  [scrollerOne setDelegate:self];
  [scrollerTwo setDelegate:self];
  [scrollerThree setDelegate:self];

  [firstView setGallery:scrollerOne];
  [secondView setGallery:scrollerTwo];
  [thirdView setGallery:scrollerThree];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  [self scrollToMatch:scrollView];
}

@end

The UITabBarController gets notified (as the scroll view's delegate) when the user scrolls one of the instances, and then calls methods like scrollToMatch: to sync up the other two with the user's choice.

Is there something that can be done, using a many-to-one relationship on IBOutlet or something like that, to narrow this down to one instance so I'm not having to manage three scroll views? I tried keeping a single instance and moving the pointer from one view to the next using the UITabBarControllerDelegate methods (calling setGallery:nil on the current and setGallery:scrollerOne on the next each time it changed), but the scroller never moved to the other tabs.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-09-02 05:01:09

当然,您应该只使用滚动视图的一个实例。而且它会正常工作,没有任何麻烦。使用方法 setGallery: 就像你所做的那样,只需确保在 setGallery 方法中将 singleScrollerForAll 视图添加到当前控制器的视图中,

-(void)setGallery:(UIView *)aScrollerView{
    [self.view addSubview:aScrollerView];
}

然后调用:

[firstView setGallery:singleScrollerForAll];

[secondView setGallery:singleScrollerForAll];

,无需在其他两个控制器中执行任何操作,因为当你调用 addSubview: 时,子视图将自动从以前的超级视图中删除。

Certainly you should use only one instance of your scroller view. And it will works fine without any troubles. Use method setGallery: like you did, just ensure you add your singleScrollerForAll view to view of current controller in setGallery method:

-(void)setGallery:(UIView *)aScrollerView{
    [self.view addSubview:aScrollerView];
}

and call:

[firstView setGallery:singleScrollerForAll];

or

[secondView setGallery:singleScrollerForAll];

and no need to do anything in other two controllers, because when you call addSubview: the subView will be automatically removed from previous superview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文