简体   繁体   English

在UITabBarController中的UIViewController之间共享UIView

[英]Sharing a UIView between UIViewControllers in a UITabBarController

I have a UIScrollView that houses a gallery of images the user can scroll through. 我有一个UIScrollView,其中包含用户可以滚动浏览的图像库。 This view needs to be visible on each of three separate UIViewControllers that are housed within a UITabBarController. 该视图必须在UITabBarController内的三个单独的UIViewController上均可见。 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. 现在,我在UITabBarController子类中有三个单独的UIScrollView实例,并且控制器设法使这三个实例保持同步(当用户滚动他们可以看到的一个实例时,以编程方式滚动其他两个实例以进行匹配等),这并不理想。

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. 我想知道是否有一种方法只能处理UIScrollView的一个实例,但只能在用户当前与之交互的UIViewController中显示它。 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): 这基本上就是我现在在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

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. 用户滚动其中一个实例时,会通知UITabBarController(作为滚动视图的委托),然后调用诸如scrollToMatch:类的方法以使其他两个实例与用户的选择同步。

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? 使用IBOutlet上的多对一关系或类似的方法,可以将范围缩小到一个实例,从而不必管理三个滚动视图吗? 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. 我尝试使用UITabBarControllerDelegate方法(在每次更改时调用setGallery:nil ,并在每次更改时在下一个调用setGallery:scrollerOne保留一个实例并将指针从一个视图移动到下一个视图,但是滚动条从未移动到其他选项卡。

Thanks in advance! 提前致谢!

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: 使用方法setGallery:与您一样,只需确保在setGallery方法中将singleScrollerForAll视图添加到当前控制器的视图即可:

-(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. 无需在其他两个控制器中执行任何操作,因为当您调用addSubview时:subView将自动从先前的Superview中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM