Umer Khan
Umer Khan

Reputation: 203

NavigationItem.rightBarButtonItem is not working

Here is Share Button Initialized:

let shareButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ShareButtonCircle"), style: .plain, target: self, action: #selector(shareButtonTapped))

Here is Objc method:

print("Share Button Clicked")
    let topicTitleAndDesc = [self]
    let vc = UIActivityViewController(activityItems: topicTitleAndDesc, applicationActivities: nil)
    present(vc, animated: true)

viewDidLoad:

self.navigationItem.rightBarButtonItem = shareButton

Showing on screen but not performing action:

screenshot

Upvotes: 0

Views: 203

Answers (2)

Trai Nguyen
Trai Nguyen

Reputation: 839

I don't face any problem with below code:

override func viewDidLoad() {
        super.viewDidLoad()
        let shareButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ShareButtonCircle"), style: .plain, target: self, action: #selector(shareButtonTapped))
        self.navigationItem.rightBarButtonItem = shareButton
}

@objc func shareButtonTapped() {
        print("Share Button Clicked")
        let topicTitleAndDesc = [self]
        let vc = UIActivityViewController(activityItems: topicTitleAndDesc, applicationActivities: nil)
        present(vc, animated: true)
    }

RESULT HERE

Upvotes: 1

Frankenstein
Frankenstein

Reputation: 16361

You are trying to share a UIViewController which is why you not getting any response. Try changing the items as follows:

let vc = UIActivityViewController(activityItems: [String(describing: self)], applicationActivities: nil)

Upvotes: 1

Related Questions