Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 645 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'#selector' refers to a method that is not exposed to Objective-C

#1
The new Xcode 7.3 passing the parameter via addTarget usually works for me but in this case it's throwing the error in the title. Any ideas? It throws another when I try to change it to @objc

Thank you!

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

The selector it's calling

func didTapCommentButton(post: Post) {
}
Reply

#2
As you know `selector`[<sup>\[About\]</sup>][1] says that `Objective-C runtime`[<sup>\[About\]</sup>][2] should be used. Declarations that are marked as `private` or `fileprivate` are not exposed to the Objective-C runtime **by default**. That is why you have two variants:

1. Mark your `private` or `fileprivate` method declaration by `@objc`[<sup>\[About\]</sup>][2]
2. Use `internal`, `public`, `open` method access modifier[<sup>\[About\]</sup>][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
In my case the function of the selector was `private`. Once I removed the `private` the error was gone. Same goes for `fileprivate`.

**In Swift 4**
You will need to add `@objc` to the function declaration. Until swift 4 this was implicitly inferred.
Reply

#4
Try having the selector point to a wrapper function, which in turn calls your delegate function. That worked for me.

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

-

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

#5
You need to use the `@objc` attribute on `didTapCommentButton(_:)` to use it with `#selector`.

You say you did that but you got another error. My guess is that the new error is that `Post` is not a type that is compatible with Objective-C. You can only expose a method to Objective-C if all of its argument types, and its return type, are compatible with Objective-C.

You could fix that by making `Post` a subclass of `NSObject`, but that's not going to matter, because the argument to `didTapCommentButton(_:)` will not be a `Post` anyway. The argument to an action function is the **sender** of the action, and that sender will be `commentButton`, which is presumably a `UIButton`. You should declare `didTapCommentButton` like this:

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

You'll then face the problem of getting the `Post` corresponding to the tapped button. There are multiple ways to get it. Here's one.

I gather (since your code says `cell.commentButton`) that you're setting up a table view (or a collection view). And since your cell has a non-standard property named `commentButton`, I assume it's a custom `UITableViewCell` subclass. So let's assume your cell is a `PostCell` declared like this:

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

// other stuff...
}

Then you can walk up the view hierarchy from the button to find the `PostCell`, and get the post from it:

@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
}


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through