2017-04-23 7 views
0

내 응용 프로그램에서 컬렉션보기에 사용하는 사용자 정의 셀의 클래스를 만들었습니다. 이 셀에는 현재 스토리 보드에서 다른 스토리 보드로 이어지고 싶은 버튼이 있습니다. 그래서 선물을 다음과 같이 사용하려고했습니다 :UICollectoinViewCell에 현재 구성원이 없습니다 (다른보기를 제공하기 위해)?

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let cr = storyboard.instantiateViewController(withIdentifier: "test") 
      self.present(cr, animated: true) 

그러나이 경우에는 자기 인 문제가 있습니다. 오류 메시지는 UICollectionViewCell에 현재의 구성원이 없음을 나타냅니다. 그러나 한 스토리 보드에서 다른 스토리 보드로 정보를 전송하기 위해 동일한 개념을 segus에 사용해야 할 수도 있으므로 어떻게하면 목표를 달성 할 수 있습니까?

+0

실제로 CustomCell에서 self.present를 호출 할 수 없으므로 UICollectionView가있는 Controller에서이 메서드를 호출하고 추가해야합니다. –

답변

1

문제에 대한 해결책은 IBOutlet

class CustomCollectionViewCell : UICollectionViewCell 
{ 
    @IBOutlet weak var btnDemo: UIButton! 
    //Otherstuff 
} 

Button를 추가 CollectionViewCell 사용자 정의에서 그런 식으로

에 고정 할 수 그리고있는 ViewControllerCollectionView는에 그 UIButton에서 IBAction를 추가 CustomCell과 CellForRowAtIndexPath에서 클릭 한 버튼을 식별하기 위해 해당 버튼에 태그를 추가해야합니다. 예 :

class ViewController : UIViewController 
{ 
    @IBAction func btnButtonPressed(_ sender: Any) 
    {   
     let currentBtn = sender as UIButton 
    } 
    func collectionView(collectionView: UICollectionView, 
     cellForItemAtIndexPath indexPath: NSIndexPath) -> 
     UICollectionViewCell 
    { 
     //Initialize Cell and other necessary stuff 
     currentCell.btnDemo.tag = indexPath.row 
    }