Programing

'#selector'는 Objective-C에 노출되지 않는 메소드를 나타냅니다.

lottogame 2020. 8. 17. 09:30
반응형

'#selector'는 Objective-C에 노출되지 않는 메소드를 나타냅니다.


addTarget을 통해 매개 변수를 전달하는 새로운 Xcode 7.3은 일반적으로 저에게 효과적이지만이 경우 제목에 오류가 발생합니다. 어떤 아이디어? @objc로 변경하려고 할 때 다른 것을 던집니다.

감사합니다!

cell.commentButton.addTarget(self, action: #selector(FeedViewController.didTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)

호출하는 선택자

func didTapCommentButton(post: Post) {
}

제 경우에는 선택기의 기능이 private. 제거하면 private오류가 사라졌습니다. 동일은 간다 fileprivate.

Swift 4에서는 함수 선언
에 추가 @objc해야합니다. 신속한 4까지 이것은 암시 적으로 추론되었습니다.


당신은 사용할 필요 @objc에 속성을 didTapCommentButton(_:)함께 사용하는을 #selector.

그렇게했다고 말했지만 또 다른 오류가 발생했습니다. 내 생각에 새로운 오류는 PostObjective-C와 호환되는 유형이 아니라는 것입니다. 모든 인수 형식과 반환 형식이 Objective-C와 호환되는 경우에만 메서드를 Objective-C에 노출 할 수 있습니다.

Post의 하위 클래스를 만들어서 고칠 수 NSObject있지만, 인수가 어쨌든 아니기 때문에 그것은 중요 didTapCommentButton(_:)하지 않습니다 Post. 행동 함수에 대한 인수는 인 송신기 동작 중, 그 송신기는 것이다 commentButton아마 인 UIButton. 다음 didTapCommentButton과 같이 선언해야 합니다.

@objc func didTapCommentButton(sender: UIButton) {
    // ...
}

그런 다음 Post탭한 버튼에 해당하는 문제에 직면하게 됩니다. 그것을 얻는 방법은 여러 가지가 있습니다. 여기 하나입니다.

나는 cell.commentButton당신이 테이블 뷰 (또는 컬렉션 뷰)를 설정하고 있다는 것을 수집합니다 (코드가라고 말했기 때문에 ). 그리고 셀에라는 비표준 속성 commentButton이 있으므로 사용자 지정 UITableViewCell하위 클래스 라고 가정합니다 . 따라서 셀 PostCell이 다음과 같이 선언 되었다고 가정 해 보겠습니다 .

class PostCell: UITableViewCell {
    @IBOutlet var commentButton: UIButton?
    var post: Post?

    // other stuff...
}

그런 다음 버튼에서 뷰 계층 구조로 올라가를 찾고 PostCell게시물을 가져올 수 있습니다.

@objc func didTapCommentButton(sender: UIButton) {
    var ancestor = sender.superview
    while ancestor != nil && !(ancestor! is PostCell) {
        ancestor = view.superview
    }
    guard let cell = ancestor as? PostCell,
        post = cell.post
        else { return }

    // Do something with post here
}

선택기가 래퍼 함수를 ​​가리 키도록하여 차례로 위임 함수를 호출합니다. 그것은 나를 위해 일했습니다.

cell.commentButton.addTarget(self, action: #selector(wrapperForDidTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)

-

func wrapperForDidTapCommentButton(post: Post) {
     FeedViewController.didTapCommentButton(post)
}

참고 URL : https://stackoverflow.com/questions/36818083/selector-refers-to-a-method-that-is-not-exposed-to-objective-c

반응형