2016-03-20 8 views
0

currentViewControllerがポップされたら、mainViewControllerUICollectionViewをcurrentViewControllerの現在の項目のインデックスにスクロールします。私はcurrentViewControllerのviewWillDisappearでこれを実装しようとしましたが、back buttonアクションが実行されたときに何も起こりません。ボタンアクションを実行した後のUICollectionView scrollToItemAtIndex

答えて

1

あなたはこのようなのViewController何かが動作するはずをポップが、を却下ないので:

childVCのviewWillDisappearのすべてをwrittigことをたくさんより管理されたプロトコルを作成してしまった
override func viewWillDisappear(animated: Bool) { 
    if let mainViewController = navigationController?.topViewController as? MainViewController { 
     mainViewController.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0), atScrollPosition: .CenteredVertically, animated: false) 
    } 
    super.viewWillDisappear(animated) 
} 
+0

これを試してみましたが、残念なことに結果は変わっていません。 – kye

+0

あなたのプロジェクトをシェアしたいですか? –

+0

確かに、私はgithubに数分でアップロードします。私はこのチャットルームhttp://chat.stackoverflow.com/rooms/106859/uicollectionview-scrolltoitematindex – kye

0

現在のViewControllerが終了すると、mainViewControllerのViewWillAppearが呼び出されます。 mainVCは、他のソースからの打ち上げだったときここでは、viewwillappearがcurrentVCから呼び出されたことを条件チェックでscrollToItemAtIndexPathを実装することができません却下されました。

+0

私は質問をする前に実装を試みました。 'presentingViewController'はnilを返された何それは' viewWillAppear' – kye

+0

内で呼び出されたたびにnilを返さ?それはcurrentVCにpushViewControllerなり、戻るボタンの上に、それはpopVCなりますmainViewControllerからなる。また、あなたは...あなたが推進しているどのように多くのコードを追加することができます。.. viewwilldisappear –

+0

'presentingViewController' – kye

0

。ご提案ありがとうございましたAndré Slotta

protocol CurrentIndexDelegate{ 
    func setCurrentIndex(index: Int?) 
} 

class ChildViewController: UIViewController{ 
    var currentIndexDelegate : CurrentIndexDelegate? = nil 
    override func viewWillDisappear(animated: Bool) { 
     super.viewWillDisappear(animated) 
     if currentIndexDelegate != nil { 
      currentIndexDelegate!.setCurrentIndex(currentIndex) 
     } 
    } 
} 

class MainViewController: UIViewController, CurrentIndexDelegate{ 
    var currentIndex: Int? 
    func setCurrentIndex(index: Int?){ 
     currentIndex = index! 
    } 
    override func viewWillAppear(animated: Bool) { 
     //Current index is set from delegate 
     if(currentIndex != nil){ 
      collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: currentIndex!, inSection: 0), atScrollPosition: .Top, animated: true) 
     } 
    } 

} 
関連する問題