I have a baseVc that all my UIViewController inherit from, all of which will have the same buttons in the UINavigationBar, if they are embedded in a UINavigationController.
What I am trying to do is set the target and action of one of these buttons from the child class, however I am having no luck. How can I get this to work?
#interface BaseVc : UIViewController
#property (strong,nonatomic) UIBarButtonItem *actionButton;
#end
Above is the header of my parent class, and in the child class:
[[super actionButton] setTarget:self];
[[super actionButton] setAction:#selector(viewMenu)];
If I've understand correctly, you should do next: assign action to the button inside the parent class, -doRightMenuButtonAction for example, and then just override this method in the child class and handle this action.
As your subclass inherits from BaseVC, it can access actionButton property. So Jjust try:
[self.actionButton setTarget:self];
[self.actionButton setAction:#selector(viewMenu)];
Make sure you are not marking your buttons as private.
class BaseViewController: UIViewController {
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRectMake(20, 200, 200, 44)
button.setTitle("Hello World", forState: .Normal)
self.view.addSubview(button)
}
}
and then in your subclass...
class AceViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: "sayHello:", forControlEvents: .TouchUpInside)
}
func sayHello (button: UIButton) {
UIAlertView(title: "Hello", message: "World", delegate: nil, cancelButtonTitle: "Ok").show()
}
}
Related
Is there any way to use addTarget on something other than self (which seems to be the most common use case)?
Yes, you can use a target other than self. The most common use is to call addTarget with self where self is a reference to the viewController that the adds the UIControl to its view hierarchy. But you aren't required to use it that way. The target is merely a reference to an object, so you can pass it a reference to any object you want. The action is a Selector which needs to be defined as an instance method on the class of that object, and that method must be available to Objective-C (marked with #objc or #IBAction) and it must take either no parameters, just the sender, or the sender and the event.
You can also pass nil as the target, which tells iOS to search up the responder chain for the action method.
Here's a little standalone example:
import UIKit
class Foo {
#objc func buttonClicked() {
print("clicked")
}
}
class ViewController: UIViewController {
let foo = Foo()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 50, y: 200, width: 100, height: 30))
button.setTitle("Press me", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(foo, action: #selector(Foo.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
}
}
You can certainly set up some other object to receive control actions. Consider the following view controller:
First, define a class who's job is to respond to button tap actions:
#objc class ButtonTarget: NSObject {
#IBAction func buttonAction(_ sender: Any) {
print("In \(#function)")
}
}
Now define a view controller that creates a ButtonTarget object
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
lazy var buttonTarget = ButtonTarget() //Give the ViewController a `ButtonTarget`
override func viewDidLoad() {
super.viewDidLoad()
//Add a taret/action to the button that invokes the method `buttonAction(_:)`
button.addTarget(
buttonTarget,
action: #selector(ButtonTarget.buttonAction(_:)),
for: .touchUpInside)
}
}
I wrote a class that shall handle UIBarButtonItem taps.
The initializer takes a reference to an UINavigationItem. All buttons etc. are attached to this UINavigationItem. I tried to connect them with actions (didPressMenuItem()), but when I click the button, the action is not triggered (nothing is written to the console nor the breakpoint I set is triggered).
How can I link the UIBarButtonItem to the function defined in this class?
internal final class NavigationBarHandler {
// MARK: Properties
private final var navigationItem: UINavigationItem?
// MARK: Initializers
required init(navigationItem: UINavigationItem?) {
self.navigationItem = navigationItem
}
internal final func setupNavigationBar() {
if let navigationItem = navigationItem {
let menuImage = UIImage(named: "menu")?.withRenderingMode(.alwaysTemplate)
let menuItem = UIBarButtonItem(image: menuImage, style: .plain, target: self, action: #selector(didPressMenuItem(sender:)))
menuItem.tintColor = .white
navigationItem.leftBarButtonItem = menuItem
}
}
#objc func didPressMenuItem(sender: UIBarButtonItem) {
print("pressed")
}
}
This is what happens in the view controller to which navigationItem the buttons etc. are attached.
class ContactsController: UIViewController {
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
self.title = "Kontakte"
let navigationBarHandler = NavigationBarHandler(navigationItem: self.navigationItem)
navigationBarHandler.setupNavigationBar()
}
}
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.
In the creation of a swift iOS app, I needed to handle the event of a UIButton press outside of the parent view controller, so I created a (very simple) protocol to delegate that responsibility to a different class:
import UIKit
protocol MyButtonProtocol {
func buttonPressed(sender: UIButton)
}
However, when I try to addTarget to a UIButton with that protocol, I get this error: Cannot convert value of type 'MyButtonProtocol' to expected argument type 'AnyObject?'. Shouldn't anything be able to be converted to AnyObject?? Here is my main code:
import UIKit
class MyView: UIView {
var delegate: MyButtonProtocol
var button: UIButton
init(delegate: MyButtonProtocol) {
self.delegate = delegate
button = UIButton()
//... formatting ...
super.init(frame: CGRect())
button.addTarget(delegate, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(button)
//... more formatting ...
}
}
Thanks in advance.
AnyObject is the protocol to which all classes conform.
To define a protocol which can only adopted by classes, add
: class to the definition:
protocol MyButtonProtocol : class {
func buttonPressed(sender: UIButton)
}
Without that modification,
var delegate: MyButtonProtocol
can be a struct or enum type and that is not convertible to AnyObject.
//i hope it will work
import UIKit
class MyView: UIView {
var delegate: MyButtonProtocol
var button: UIButton
init(delegate: MyButtonProtocol) {
self.delegate = delegate
button = UIButton()
//... formatting ...
super.init(frame: CGRect())
button.addTarget(delegate, action: Selector("buttonPressed:") forControlEvents: .TouchUpInside)
addSubview(button)
//... more formatting ...
}
}
I have 2 classes, the first class is named "store" where I create a button which calls a method: "storeSelected" located in the second class with the name ExploreViewController.
The method should print "done" and take me to another view controller.
Without the segue "done" is printed but when putting the segue code the app crashes.
The error is:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<Glam.ExploreViewController: 0x14ed3be20>) has no segue with identifier 'ok''
.........
// ExploreViewController Class
let _sharedMonitor: ExploreViewController = { ExploreViewController() }()
class ExploreViewController: UIViewController, UIScrollViewDelegate {
class func sharedMonitor() -> ExploreViewController {
return _sharedMonitor
}
func storeSelected(sender: UIButton) {
println("done") // it entered here and "done" is printed
self.performSegueWithIdentifier("ok", sender: self) //here is the problem
}
}
// another class named "Store"
// button is created
let monitor = ExploreViewController.sharedMonitor()
btn.addTarget(monitor, action: Selector("storeSelected:"), forControlEvents: UIControlEvents.TouchUpInside)
Assuming that you have a view controller where you create the Store object - you should pass the button action back to this view controller and add a segue from it to your desired destination.
Best practice is to use a protocol that delegates the buttons action back up to the viewController that it is contained in as below.
Store.swift
protocol StoreDelegate: NSObject {
func didPressButton(button: UIButton)
}
class Store: UIView {
weak var delegate: StoreDelegate!
override init(frame: CGRect) {
super.init(frame: frame)
var button = UIButton()
button.setTitle(
"button",
forState: .Normal
)
button.addTarget(
self,
action: "buttonPress:",
forControlEvents: .TouchUpInside
)
self.addSubview(button)
}
func buttonPress(button: UIButton) {
delegate.didPressButton(button)
}
}
ViewController.swift
class ViewController: UIViewController, StoreDelegate {
override func viewDidLoad() {
super.viewDidLoad()
addStoreObj()
}
func addStoreObj() {
var store = Store()
store.delegate = self // IMPORTANT
self.view.addSubview(store)
}
func didPressButton(button: UIButton) {
self.performSegueWithIdentifier(
"ok",
sender: nil
)
}
}
This code is untested, but I hope you get the idea - your Store object delegates the button press activity back to its containing ViewController and then the ViewController carries out the segue that you have attached to it in the Storyboard.
I am creating a UIButton in another file opposed to the main ViewController. I created this button
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
newNoteButton.setTitle("New Note", forState: UIControlState.Normal)
newNoteButton.userInteractionEnabled = true
self.view.addSubview(newNoteButton)
with action
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
It is throwing the above error even though, if i copy and paste the same code into my ViewController, it doesn't flag me at all. Why is it doing this? It is meant to just print out the string "New Note" but something causes Thread 1 to queue.
Edit:
After a bit of reading, I have been able to reduce the amount of code to just this:
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
and this:
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
I tried removing the action and the problem was still there. This exists within my textView class in a separate file. In my AppDelegate file, the Root View Controller is ViewController. If I move the button to ViewController, the issue does not present itself. The only code in my ViewController is this
import Foundation
import UIKit
class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
//Global Variables
var scrollView = UIScrollView()
override func viewDidLoad(){
super.viewDidLoad()
//Add textView
//let textViewRef = textView() Removed to test
//self.view.addSubview(textViewRef.view)
//Button Code
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newNoteButton)
}
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
}
I have tried messing about with conditionals and the semicolon in the action but that doesn't seem to affect it or help in any way. If you need any more information, please feel free to ask.
Edit 2
My other View Controller looks like this:
import Foundation
import UIKit
class textView: UIViewController {
//Globals
var newNoteButton = UIButton()
override func viewDidLoad()
newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newNoteButton)
}
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
}
Edit 3: I just noticed that this information might be a bit relevant. The error is on the class AppDelegate line in my delegate file.
I had the same problem and I received the same error that you. I wanted create a button in other class. I recommend you read my solution: How invoke a method in the method addTarget of the Button class when I create programmatically the button in a plain class in Swift . An abstract, you should inherit of the "UIClass" that you require. If you want to create a button, you should inherit of "UIButton" class. Then, you set the values of the attributes of this class instead of create a button.
FYI for anybody with this issue in the future, it can usually be debugged back to the retention of the button within the class you are adding the target to.
Try moving the declaration of the button or targeted class up to a class global to be cleaned up after the view is disposed of.
Example: In the instance listed above, declare the newNoteButton at the class level instead of inside the viewDidLoad function to ensure it's retained while it can call it's target.
class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {
// Global Variables
var scrollView = UIScrollView()
var newNoteButton: UIButton! // <- Declare button here instead of in viewDidLoad
}