2014-06-19 4 views
2

의 UIView의 속성에 UISwipeGestureRecognizer 추가 나는 초기화 방법입니다있는 UITableViewCell 클래스는 다음과 같습니다 사용자 정의가 있습니다. 문제는 제스처를 추가 할 때 self.topLayerViewnil 인 인식 자입니다.가있는 UITableViewCell 사용자 정의 클래스

나는 init 방법 같은 것을 작성하는 경우 :

if self.topLayerView? { 
      println("NOT empty") 
     } else { 
      println("empty") 
     } 

항상 나에게 "빈"제공합니다. 내 질문은 :이 코드를 대신 넣는 것이 적절한가요?

도움이 될 것입니다.

답변

3

IBOutlet이 설정되기를 기대하기에는 Init의 수명주기가 너무 이르다. 코드를 인터페이스 파일이 완전히로드 된 후 호출되는 셀의 awakeFromNib() 메서드로 이동하는 것이 좋습니다. 이 방법과 다른 방법에 대한 자세한 내용은 NSObject UIKit Additions Reference에서 확인할 수 있습니다.

override func awakeFromNib() { 
    super.awakeFromNib() 

    let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedLeft") 
    let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedRight") 

    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 

    self.topLayerView?.addGestureRecognizer(swipeLeft) 
    self.topLayerView?.addGestureRecognizer(swipeRight) 
} 
+1

브릴리언트. 고마워요! :) –

+0

이 예제에서 swipeLeft/swipeRight가 실행될 때 실행되는 코드 블록/액션은 어디에 있습니까? 셀 VC 또는 테이블 VC에 해당 코드가 있습니까? 덕분에 –

+0

. 나를 위해 일했다. – asdr

관련 문제