Ios 如何从另一个控制器启用滚动?
我有两个控制器:ViewController和GalleryViewController(上面有集合视图)。从GalleryViewController中我为collectionView设置的情节提要Ios 如何从另一个控制器启用滚动?,ios,swift,uiviewcontroller,uicollectionview,Ios,Swift,Uiviewcontroller,Uicollectionview,我有两个控制器:ViewController和GalleryViewController(上面有集合视图)。从GalleryViewController中我为collectionView设置的情节提要已启用滚动到false。现在,如何从ViewController将其更改为true 我试过这个: var vc: GalleryViewController? vc.collectionView.scrollEnabled = true 但它不起作用。是否有其他解决方案可用于更改其他控制器(Vie
已启用滚动
到false
。现在,如何从ViewController
将其更改为true
我试过这个:
var vc: GalleryViewController?
vc.collectionView.scrollEnabled = true
但它不起作用。是否有其他解决方案可用于更改其他控制器(ViewController)的滚动?您的代码将创建GalleryViewController的新实例,您需要使用现有实例 您有许多选项,部分取决于如何从
ViewController
导航到GalleryViewController
如果您是从初始控制器创建Gallery视图,那么应该使用prepareForSegue
,类似于
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue!.identifier == "GallerySegueOrWhateverYouHaveCalledIt" {
let viewGalleryController:ViewGalleryController = segue!.destinationViewController as ViewGalleryController
let collectionViewLink = viewGalleryController.collectionView
}
}
for vc in self.childViewControllers
{
if vc.isKindOfClass(GalleryViewController)
{
myGalleryViewController = vc as! GalleryViewController
}
}
如果您使用的是选项卡控制器,并且假设您知道GalleryView的索引,那么我们将其称为indexGalleryView
,这样就更容易了
var vc = tabBarController!.viewControllers![indexGalleryView] as! GalleryViewController
vc.collectionView.scrollEnabled = true
如果您有一个ViewController->Container->embedded GalleryViewController->CollectionView,您可以在顶级控制器的viewDidLoad中获得嵌入式ViewController的句柄,如下所示
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue!.identifier == "GallerySegueOrWhateverYouHaveCalledIt" {
let viewGalleryController:ViewGalleryController = segue!.destinationViewController as ViewGalleryController
let collectionViewLink = viewGalleryController.collectionView
}
}
for vc in self.childViewControllers
{
if vc.isKindOfClass(GalleryViewController)
{
myGalleryViewController = vc as! GalleryViewController
}
}
拥有
myGalleryViewController
后,您应该能够访问子视图上的所有内容否,它是ViewController中的嵌入式控制器。我认为这种方法不适用于它=/oh不,ViewController在TabBar上,但GalleryVC作为嵌入式VCC位于ViewController内部,然后将GalleryVC的插座添加到ViewController并直接访问它我已将“容器”放入ViewController中,并放入那些容器中GalleryViewController
。如何将“GalleryVC”的出口添加到VC?抱歉,这是我第一次使用嵌入式控制器,因为我无法通过拖动在VC上创建GalleryVC的出口