#selector on UIBarButtonItem - ios

I'm developing my first iOS app in Swift 2.2 and I have the following problem.
In an utility class, I have the following static method, called by some different UIViewController.
static func setNavigationControllerStatusBar(myView: UIViewController, title: String, color: CIColor, style: UIBarStyle) {
let navigation = myView.navigationController!
navigation.navigationBar.barStyle = style
navigation.navigationBar.barTintColor = UIColor(CIColor: color)
navigation.navigationBar.translucent = false
navigation.navigationBar.tintColor = UIColor.whiteColor()
myView.navigationItem.title = title
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: self, action: #selector("Utils.menuClicked(_:)"))
myView.navigationItem.leftBarButtonItem = menuButton
}
func menuClicked(sender: UIButton!) {
// do stuff
}
I'm trying in some different ways to associate a #selector for this button, however I always have the following error.

No quotes.
#selector(Utils.menuClicked(_:))
func menuClicked should be in your view controller class. But if for some reason it isn't, you can do
class Utils {
static let instance = Utils()
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: Utils.instance, action: #selector(Utils.menuClicked(_:)))
#objc func menuClicked(sender: UIBarButtonItem) {
// do stuff
}
}

Swift 2.2 deprecates using strings for selectors and instead introduces new syntax: #selector.
Using #selector will check your code at compile time to make sure the method you want to call actually exists. Even better, if the method doesn’t exist, you’ll get a compile error: Xcode will refuse to build your app, thus banishing to oblivion another possible source of bugs.
So remove the double quote for your method in #selector. It should work!

Related

store #selector in a variable

I'm adding a target to a button but instead of having the target action reference a predefined function I want it to reference a closure.
typealias AlertAction = (title: String, handler: () -> ())
class ErrorView: UIView {
func addAction(_ action: AlertAction) {
let button = UIButton()
...
let selector = #selector(action.handler) //error happens here
button.addTarget(self, action: selector, for: .touchUpInside)
}
}
I'm getting an error on this line:
let selector = #selector(action.handler)
which is "Argument of '#selector' does not refer to an '#objc' method, property, or initializer"
This makes sense because usually you have to add #objc to your func declaration, but I'm wondering if there's a way to make my closure refer to an #objc method after the fact perhaps by wrapping it in another function.
Is this possible? I don't know how to define an #objc marked closure so I'm not sure.
#selector() is based on Objective-C bridging, since swift closures are non-objective-c, you can't use them.
One alternative solution is wrap your code inside an Objective-C function.
class AlertAction:NSObject {
var title:String?
#objc
func getHandler(sender:Any){
print("hi")
}
}
and use it like:
// Instance of a class
let alertAction = AlertAction()
// Usage
let button = UIButton()
let selector1 = #selector(AlertAction.getHandler)
button.addTarget(alertAction, action: selector1, for: .touchUpInside)

Toolbar with "Previous" and "Next" for keyboard

I've been trying to implement this toolbar, where only the 'Next' button is enabled when the top textField is the firstResponder and only the 'Previous' button is enabled when the bottom textField is the firstResponder.
It kind of works, but i need to execute my own code by accessing previous, next and done buttons action methods in other classes(like delegates)
Thanks in advance for your suggestions..
extension UIViewController {
func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false) {
for (index, textField) in textFields.enumerated() {
let toolbar: UIToolbar = UIToolbar()
toolbar.sizeToFit()
var items = [UIBarButtonItem]()
if previousNextable {
let previousButton = UIBarButtonItem(image: UIImage(named: "Backward Arrow"), style: .plain, target: nil, action: nil)
previousButton.width = 30
if textField == textFields.first {
previousButton.isEnabled = false
} else {
previousButton.target = textFields[index - 1]
previousButton.action = #selector(UITextField.becomeFirstResponder)
}
let nextButton = UIBarButtonItem(image: UIImage(named: "Forward Arrow"), style: .plain, target: nil, action: nil)
nextButton.width = 30
if textField == textFields.last {
nextButton.isEnabled = false
} else {
nextButton.target = textFields[index + 1]
nextButton.action = #selector(UITextField.becomeFirstResponder)
}
items.append(contentsOf: [previousButton, nextButton])
}
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing))
items.append(contentsOf: [spacer, doneButton])
toolbar.setItems(items, animated: false)
textField.inputAccessoryView = toolbar
}
}
}
I am calling this from other class as :
let field1 = UITextField()
let field2 = UITextField()
addInputAccessoryForTextFields([field1, field2], dismissable: true, previousNextable: true)
Although I'm not 100% convinced I understand your question, here goes:
From other classes, you want to call the actions of your buttons, but your actions are set to UITextField.becomeFirstResponder and UIView.endEditing.
Rather than call these methods directly, create your own methods the actions should call, and put these calls into those methods.
In addInputAccessoryForTextFields(...) change the previousButton's target and action to:
previousButton.target = self
previousButton.action = #selector(handlePreviousButton)
Now add the new method:
#objc func handlePreviousButton()
{
// you'll need to associate the previous button to a specific text field
// and hang onto that association in your class, such as in a property named textFieldRelatedToPreviousButton.
self.textFieldRelatedToPreviousButton.becomeFirstResponder()
}
Now you can call handlePreviousButton() directly from elsewhere in your class, if you wish, or even from other classes.
Update
I just noticed you're extending UIViewController. So you can't add storage by adding a property. You can add storage via objc_setAssociatedObject and then get it via objc_getAssociatedObject, however, to get around this. See this SO or this SO for details on that. So you can, for example, "attach" the textField to your previousButton so that you can access it via the handlePreviousButton() method you add to your extension. And you can pass in the previousButton as a parameter (the sender) to handlePreviousButton() too.
Update 2
Another approach to consider is to use the button's tag property to store the tag value of the related textField. (i.e. each button and its related textField would have the same tag value). So in handlePreviousButton(sender:UIBarButtonItem) you loop through all the UITextField children of your self.view and locate the one whose tag matches sender.tag . Then you can do what you need to that UITextField.

Get the View of UIBarButtonItem in Swift

Hopefully, this is an easy question. I am trying to obtain the UIView of a UIBarButtonItem. Looking at the following StackOverflow question I came up with the following code:
let notificationButton = UIBarButtonItem(image: UIImage(named: "icon_notification.png"), style: .plain, target: self, action: nil)
let notificationView = notificationButton.value(forKey: "view") as? UIView
However, notificationView is nil. So, thoughts on what I failed to interpret from the linked StackOverflow question?
Starting from iOS 11 this code will not work cause line of code below will not cast UIView. Also it's counting as private API and seems to be will not pass AppStore review.
guard let view = self.value(forKey: "view") as? UIView else { return }
Thread on: forums.developer.apple
So, DS Dharma gave me a idea which ended up working. The "view" value is only available after it is assigned to the toolbar navigation item like this:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_notification.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem?.addBadge(number: 0, withOffset: CGPoint(x: 7.0, y: 0.0) , andColor: UIColor.black, andFilled: true)
where the addBadge() function needs the UIView. BTW, if anyone is wondering, the addBadge function was taken from this post: http://www.stefanovettor.com/2016/04/30/adding-badge-uibarbuttonitem
Highly recommended if you need this piece of functionality.

No method declared with Objective-C selector 'methodName()' in Swift 2.2 [duplicate]

This question already has answers here:
Xcode 7.3 / Swift 2: "No method declared with Objective-C selector" warning
(4 answers)
Closed 6 years ago.
I setup a UIToolBar inside a function named toolBarSetup inside a class.
public class Utility {
func toolBarSetup(inout toolBar: UIToolbar, inout toolBarLbl: UILabel, view: UIView) -> (UIToolbar, UILabel){
toolBar = UIToolbar(frame: CGRectMake(0, view.frame.height/7, view.frame.width, 44.0))
let toolBar_btn = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "picker_cancel")
//Codes to setup toolbar label
toolBar.setItems([toolBar_btn, flexSpace, text_info, flexSpace], animated: true)
return (toolBar, lbl_toolBar_cancel)
}
}
From another class I'm calling this function
class Class1: UIViewController {
var toolBar = UIToolbar()
var lbl_toolBar = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
let toolBarSetup = Utility().toolBarSetup(&toolBar, lbl_toolBar: &lbl_toolBar, view: view)
toolBar = toolBarSetup.0
lbl_toolBar = toolBarSetup.1
}
func picker_cancel(){
}
}
Earlier the function picker_cancel() was working fine but yesterday I have updated my Xcode after that I'm getting this warning
No method declared with Objective-C selector 'picker_cancel()'
in the below line of class 'Utility'.
let toolBar_btn = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "picker_cancel")
I tried to solve by using selector but nothing worked. Please help.
In Xcode 7.3 you need to use the #selector(). Normally Xcode can do this change for you.
I think that works:
#selector(Class1.picker_cancel)

Swift: Is there a cleaner way to pass an extra UIBarButtonItem action: Selector parameter other than using button.tag?

I'm currently working on a snapchat-like menu where clicking the left and right UIBarButtonItem makes the screen go in their respective directions.
TL;DR - I'm wondering if there's a (clean) built-in way of passing through a tag as an Optional type to avoid crashes.
override func viewDidLoad() {
super.viewDidLoad()
// Other setup code here
let leftButton = UIBarButtonItem(title: leftButtonString, style: UIBarButtonItemStyle.Plain, target: self, action: "navButtonClicked:")
let rightButton = UIBarButtonItem(title: rightButtonString, style: UIBarButtonItemStyle.Plain, target: self, action: "navButtonClicked:")
// These tags are not a good solution because they aren't optionals!
leftButton.tag = 0
rightButton.tag = 1 // this isn't necessary, but I don't want it to crash...
// More setup here
}
func navButtonClicked(sender: UIBarButtonItem) {
// Goes right by default
let currentX = self.parentScrollView!.contentOffset.x
var screenDelta = self.parentScrollView!.frame.width
if sender.tag == 0 {
screenDelta *= -1
}
UIView.animateWithDuration(0.5, animations: {() in
self.parentScrollView!.contentOffset = CGPoint(x: currentX + screenDelta, y: 0)
})
}
My current solution works, I'm just working towards writing cleaner code.
Thanks!
Option 1:
Create two properties in your view controller that correspond to each UIBarButtonItem. This way you'll be able to tell which one was tapped.
Option 2:
Sublass UIBarButtonItem and add a property that you want.

Resources