UIBarButtonItem action not work UITableView cell
Use this :
Inside
UIBarButtonItem(title: Wordings.BTN_CHOOSE, style:UIBarButtonItemStyle.done, target: nil, action: #selector(self.pickerChildrenDonePressed)
change target to self
instead of nil
@IBAction of UIBarButtonItem not being called from UIToolbar inside generic cell of tableview
Ok, so the problem was that I forget to mark the UIToolbar
as lazy
so I couldn't use class properties and functions.
I don't know why Xcode didn't warn me about this... I think this could be reported.
UIBarButtonItem not firing up selector action
The code you provided should be working so you need to provide more context (show more of your view controller class where this is implemented).
UIKit provides a set of standard UIBarButtonItems called system items. These happen to include an 'add' item, which is displayed as a plus-sign and used throughout iOS. Using this item solves question 2 and 3.
UIBarButtonItem has a convenience constructor for initialising a system item. See the working example below:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let addProdButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addProduct))
navigationItem.leftBarButtonItem = addProdButton
}
@objc func addProduct() {
print("addProduct() called")
}
}
Action not being triggered for UIBarButtonItem
You are initialising the UIToolbar
without a frame and that would make it not register any touch events because they would be out of the toolbar's bounds.
Replace let accessoryView = UIToolbar()
with something like let accessoryView = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44))
Or you can call accessoryView.sizeToFit()
before return accessoryView
Why does a UIBarButtonItem action not trigger in a separate class?
Th problem here is that you're instantiating NavigationBarHandler
inside viewDidload()
which is why the memory reference dies after viewDidLoad()
finishes. What you should do is to create the variable outside like this.
class ContactsController: UIViewController {
var navigationBarHandler: NavigationBarHandler!
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
self.title = "Kontakte"
self.navigationBarHandler = NavigationBarHandler(navigationItem: self.navigationItem)
navigationBarHandler.setupNavigationBar()
}
}
This way the memory reference stays.
UIBarButtonItem Action created in another class not firing (swift)
Echoing what Huy Nghia mentioned in the comments, if you set the target to nil, you've effectively send a message to a nil object, which does nothing.
[nil doneButtonAction]
is effectively a no-op
Related Topics
Realm Error Domain=Io.Realm.Unknown Code=89 "Operation Canceled"
Example for Drag and Drop Inside Nscollectionview
How to Add Characters into Dateformatter
Error: Argument Type Double/String etc. Does Not Conform to Expected Type "Anyobject"
How Could I Request Text from a Website in Swift
Hiding Dividers in Nssplitview
How Is a Global Variable Set to Private Understood in Swift
How to Get All Characters of the Font with Ctfontcopycharacterset() in Swift
Swift How to Sort Dict Keys by Byte Value and Not Alphabetically
Value of Optional Type 'Nsurl' Not Unwrapped; Did You Mean to Use '!' or ''
Swift Tuple Has Unexpected Print Result
How to Create a Dynamic Link in Firebase Programmatically? Swift
How to Change the Order of Functions Triggered
"Ambiguous Use of 'Children'" When Trying to Use Nstreecontroller.Arrangedobjects in Swift 3.0
Is There Difference Between Using a Constructor and Using .Init
How to Set Cadisplaylink in Swift with Weak Reference Between Target and Cadisplaylink Instance