I am new to swift and trying to create a UIButton programmatically. I have two classes. I am creating a button in one class and calling that method from another class.
class viewcontroller
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let renderobj = render()
renderobj.sign()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class render
import UIKit
class render: UIViewController {
func sign() {
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender: UIButton!) {
print("Button tapped")
}
}
But no button is created. If I put the UIButton code inside viewDidLoad() , it works. I want the button to be created in a function of different class. Is it possible to do so ?
Thanks for any help
func sign() -> UIButton
{
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
return button
}
After returning that button you can add it in your calling view controller view
override func viewDidLoad() {
super.viewDidLoad()
let renderobj = render()
let button = renderobj.sign()
self.view.addSubview(button)
}
After doing this
let renderobj = render()
And after calling this function, a new button is initialized and added to the view of render view controller.
renderobj.sign()
But renver view controller is only initialized and its view is not added to current view controller which is ViewController
If you want to show render controller, then present it like usual. If you want to add that button to your ViewController then either declare a button variable and access it or return it in sign() function
I think there is a lack of understanding around the concepts of a UIViewController here. A UIViewController is used to manage the views displayed to a user, think about it as a single screen of your app. You can embed view controllers in other view controllers, but typically you won't need to do this for basic stuff.
A UIViewController has a lifecycle which you can find more about here: https://developer.apple.com/reference/uikit/uiviewcontroller#1652793
Now what it appears like you want to do is to wrap up the functionality of creating a button, you can do this by either creating a factory method in you ViewController class (or other class somewhere else), or by subclassing UIButton and creating a convenience initialiser method to create the button with the properties you want (you can also use Swift extensions but I won't go into that here).
Lets look at the simplest method and create a factory method inside your ViewController class:
// The button method takes a title and a selector (the selector is the method that should be called when the button is tapped)
func makeButton(title: String, action: Selector) -> UIButton {
// Create a basic button with a default size
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
// Customise the button with your settings
button.backgroundColor = .green
button.setTitle(title, for: .normal)
button.addTarget(self, action: action, for: .touchUpInside)
// Return the newly created button
return button
}
Now we have a method that creates a button of a default size and position, we want to call that method to create a button and then add it as a subview of the ViewController's view. To do that we can do something in the viewDidLoad() method that we override:
override func viewDidLoad() {
// Always remember to call the super method to ensure all super classes have a chance to do any work thats required by them
super.viewDidLoad()
// Create the button by calling our new method
let button = self.makeButton(title: "Test Button", action: #selector(buttonTapped))
// Set the origin of the button to move it to the right position
button.frame.origin = CGPoint(x: 100, y: 100)
// Finally add the new button as a subclass of the view controllers view
self.view.addSubview(button)
}
Now we understand whats going on in the methods we can put it all together to have a class that looks like this:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = self.makeButton(title: "Test Button", action: #selector(buttonTapped))
button.frame.origin = CGPoint(x: 100, y: 100)
self.view.addSubview(button)
}
func makeButton(title: String, action: Selector) -> UIButton {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle(title, for: .normal)
button.addTarget(self, action: action, for: .touchUpInside)
return button
}
func buttonTapped(sender: UIButton) {
print("Button tapped")
}
}
You create new UIViewController here:
let renderobj = render()
renderobj.sign()
but ViewController instance is showing in this moment.
Related
The code below compiles fine, but crashes with an unrecognized selector sent to instance error.
I have one class that inherits from UIViewController:
class Controller: UIViewController {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
... Other code ...
}
}
And another class that is just a wrapper for a UIView and contains buttons:
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
let button = UIButton()
... Some button layout code ...
button.addTarget(target, action: #selector(CustomToolbar.buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc static func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
For the sake of clarity, I left out a large chunk of code and kept what I thought was necessary. I think that my code doesn't work because of my misunderstanding of the how the target works in the addTarget function. Normally, I would just use self as the target of my button's action, so I just tried to pass along self from the view controller to the CustomToolbarWrapper's init function.
What else I have tried:
Changing the button's target from target to self like this:
button.addTarget(self, action: #selector(CustomToolbar.buttonTapped(_:)), for: .touchUpInside)
results in the app not crashing anymore. Instead, however, I believe that line of code fails to do anything (which doesn't throw an error for some reason?) because attempting to print button.allTargets or even button.allTargets.count results in the app crashing at compile time, with an EXC_BREAKPOINT error and no error description in the console or the XCode UI (which just confuses me even more because there are no breakpoints in my code!).
Also, making buttonPressed(_:) non-static does not change any of the previously mentioned observations.
Also, to make sure the button could in fact be interacted with, I added this in the viewDidLoad() of Controller:
for subview in toolbar.subviews? {
if let button = subview as? UIButton {
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
}
}
and added a simple testing method to Controller for the button:
#objc func buttonPressed(_ sender: UIButton) {
print("Button Pressed")
}
And running the code did result in "Button Pressed" being printed in the console log, so the button should be able to be interacted with by the user.
Feel free to let me know if you think this is not enough code to figure out the problem, and I will post more details.
Edit
I prefer to keep the implementation of the button's action in the CustomToolbarWrapper class to prevent repeating code in the future, since the action will be the same no matter where an instance of CustomToolbarWrapper is created.
The best option would be to add the target in your controller and then call a method in your toolbarWrapper on button press. But if you really need to keep this design, you should have a strong reference to your toolbarWrapper in your controller class, otherwise your toolbarWrapper is deallocated and nothing gets called. Also, the buttonTapped(_:) method does not need to be static. Thus, in your controller:
class Controller: UIViewController {
var toolbarWrapper: CustomToolbarWrapper?
override func viewDidLoad() {
toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
... Other code ...
}
}
And in your wrapper:
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height,width: view.frame.width, height: height))
let button = UIButton()
... Some button layout code ...
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
There is another way I would use which is delegation. The target does not necessarily have to be a controller, it can be the CustomToolbarWrapper itself.
First, declare a protocol
protocol CTDelegate: AnyObject {
func didClickButton()
}
Then in CustomToolbarWrapper add a property, weak var delegate: CTDelegate? and a button action:
#objc func buttonTapped(_ sender: UIButton) {
delegate?.didClickButton()
}
So in your case, it becomes:
button.addTarget(self, action: #selector(CustomToolbarWrapper.buttonTapped(_:)), for: .touchUpInside)
Then when you go to any ViewController, conform to CTDelegate and initialize the CustomToolbarWrapper, you can set its delegate to the controller.
e.g
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
toolbarWrapper.delegate = self
and implement your action inside the method you are conforming to in your controller i.e.
func didClickButton()
Your problem is right here:
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
You're passing an instance of Controller class which doesn't implement the buttonTapped(_:) selector. It is implemented by your CustomToolbarWrapper class. This is a bad design in general. You should either follow a delegate pattern, or a callback pattern.
Updated Answer:
Delegate pattern solution:
class Controller: UIViewController, CustomToolbarWrapperDelegate {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, buttonDelegate: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
}
// MARK: - CustomToolbarWrapperDelegate
func buttonTapped(inToolbar toolbar: CustomToolbarWrapper) {
print("button tapped")
}
}
protocol CustomToolbarWrapperDelegate: AnyObject {
func buttonTapped(inToolbar toolbar: CustomToolbarWrapper) -> Void
}
class CustomToolbarWrapper {
var toolbarView: UIView
weak var buttonDelegate: CustomToolbarWrapperDelegate?
init(view: UIView, buttonDelegate: CustomToolbarWrapperDelegate?) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
self.buttonDelegate = buttonDelegate
let button = UIButton()
button.addTarget(self, action: #selector(self.buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc private func buttonTapped(_ sender: Any) {
// Your button's logic here. Then call the delegate:
self.buttonDelegate?.buttonTapped(inToolbar: self)
}
}
If you'd rather stick to your current design then just implement the following changes:
class Controller: UIViewController {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self, selector: #selector(self.buttonTapped(_:)), events: .touchUpInside)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
}
#objc private func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any?, selector: Selector, events: UIControlEvents) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
let button = UIButton()
button.addTarget(target, action: selector, for: events)
toolbarView.addSubview(button)
}
}
I am trying to add a delete button as a subview in an image. This is my current structure:
-> class DesignViewController: UIViewController
|
-> class Sticker: UIImageView, UIGestureRecognizerDelegate
|
-> UI button inside the Sticker
Inside Sticker class I have :
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let button2 = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button2.backgroundColor = .red
button2.setTitle("Delete", for: .normal)
button2.tag = 23
button2.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.addSubview(button2)
}
#objc func buttonAction(sender: UIButton!) {
print("Button tapped")
}
The buttonAction is not getting called.
When I change self.addSubview(button2) line to :
self.superview?.addSubview(button2)
I can see buttonAction getting called. However I would like to keep the button inside the Sticker view so that when user moves the sticker, the button moves as a subview with it.
Can anyone please help and let me know how I can keep the button inside Sticker view?
By default isUserInteractionEnabled property of UIImageView is set to false. Set it to true and your button will start to respond. You can set it in code as well as in the storyboards.
Also try setting the clipsToBounds property of your imageview to true. It will clip your button if it is going outside of the image bounds. That might be one of the reason that your button is not getting touches.
You should create a protocol delegate for button action. This is code example:
protocol ButtonDelegate: class {
func buttonTapped(button: UIButton)
}
class Sticker: UIImageView {
weak var delegate: ButtonDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(button2)
}
lazy var button2: UIButton = {
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button2.backgroundColor = .red
button2.setTitle("Delete", for: .normal)
button2.tag = 23
button2.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
return button
}()
#objc func buttonAction(sender: UIButton) {
guard let delegate = delegate else { return }
delegate.buttonTapped(button: sender)
}
So, now go to your DesignViewControllerl, add your custom imageview class Sticker. Don't forget to do that "imageView.delegate = self". Then in extension add protocol delegate you've created before. Code example:
class DesignViewController: UIViewController {
private lazy var sticker: Sticker = {
let iv = Sticker(frame: view.bounds)
iv.delegate = self
return iv
}()
override viewDidLoad() {
super.viewDidLoad()
view.addSubiew(sticker)
}
}
extension DesignViewController: ButtonDelegate {
func buttonTapped(button: UIButton) {
// input your action here
}
}
I have a ButtonProvider class that creates my button and adds a target action. However, when the button is pressed, it doesn't call the function from the ButtonProvider class in which it was created, but instead calls a function with the same name from the other class.
class ButtonProvider {
let view: UIView!
let button: UIButton!
init(view: UIView) {
self.view = view
}
func showButton() {
button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.backgroundColor = .white
button.addTarget(self, action: #selector(ButtonProvider.close), for: .touchUpInside)
view.addSubview(button)
}
#objc func close() {
button.removeFromSuperview()
}
}
And in my calling ViewController:
override func viewDidLoad() {
let buttonProvider = ButtonProvider(view: self.view)
buttonProvider.showButton()
}
#objc func close() {
//This is the function that is called
}
Any ideas why a selector of ButtonProvider.close calls my ViewController.close function?
You buttonProvider is not owned by the controller therefore it will be deallocated as soon as viewDidLoad ends. When the button is pressed the instance does not exist in memory any more and the result will be undefined behavior, usually a crash.
You will have to save buttonProvider to a strong property in the controller.
I have programmatically created a button in my main class and passing an instance of a game class (gameSCNScene - where most of the game logic lies) to the button. Inside this game class instance is where the action for the button resides however when ever I press the button I get the error - Unrecognized selector.
class GameViewController: UIViewController, SCNSceneRendererDelegate {
var gameSCNScene: GameSCNScene!
override func viewDidLoad() {
super.viewDidLoad()
let scnView = self.view as! SCNView
scnView.delegate = self
// Create my game scene instance
gameSCNScene = GameSCNScene(currentview: scnView)
// Make button
makeButtonsUI(gameSCNScene)
}
func makeButtonsUI(gameSCNScene: GameSCNScene) {
let image = UIImage(named: "art.scnassets/addBtn.png") as UIImage?
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(image, forState: .Normal)
button.addTarget(self, action:("gameSCNScene.addCube:"), forControlEvents:.TouchUpInside)
self.view.addSubview(button)
}
Button function inside my gameSCNScene instance
func addCube(sender:UIButton) {
//Code here
}
The line of code where you add the target is incorrect. This:
button.addTarget(self, action:("gameSCNScene.addCube:"), forControlEvents:.TouchUpInside)
Should be:
button.addTarget(gameSCNScene, action:("addCube:"), forControlEvents:.TouchUpInside)
I wonder if I can trigger an action with a button created in an another class.
I explain:
I have two classes, the view controller, and a class used to create a view.
In the view controller, I call a method located in the second class to create a custom view. Then I add the custom view to the main view (see code below).
The custom view displays a button and I don't know how I can use my button because the method created as a target is not found when I run my app.
Code viewController:
import UIKit
class FirstViewController: UIViewController {
var popupViewBeforeOrderCoupon:UIView?
override func viewDidLoad() {
super.viewDidLoad()
popupViewBeforeOrderCoupon = CustomView.createPopupViewWithList()
self.view.addSubview(popupViewBeforeOrderCoupon!)
}
func cancelView(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
}
and the second class CustomView:
import Foundation
import UIKit
class CustomView {
init () {
}
static func createPopupViewWithList() -> UIView? {
var dynamicView = UIView(frame: CGRectMake(100, 200, 200, 100))
dynamicView.backgroundColor = UIColor.grayColor()
dynamicView.alpha = 1
dynamicView.layer.cornerRadius = 5
dynamicView.layer.borderWidth = 2
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(10, 10, 100, 50)
dynamicView.addSubview(button)
button.addTarget(self, action: "cancelView:", forControlEvents: .TouchUpInside)
return dynamicView
}
func cancelView(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
}
I want my button created in customView to call the method cancelView when I press it but I don't manage to do it.
Here's the error I get :
NSForwarding: warning: object 0x523c8 of class 'Myproject.CustomView' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector +[Myproject.CustomView cancelView:]
How can I do it?
You can modify your createPopupViewWithList to accept 1 argument of UIViewController and then set button's target to it.
Code:
static func createPopupViewWithList(controller: UIViewController) -> UIView? {
var dynamicView = UIView(frame: CGRectMake(100, 200, 200, 100))
dynamicView.backgroundColor = UIColor.grayColor()
dynamicView.alpha = 1
dynamicView.layer.cornerRadius = 5
dynamicView.layer.borderWidth = 2
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(10, 10, 100, 50)
dynamicView.addSubview(button)
// set your controller here
button.addTarget(controller, action: "cancelView:", forControlEvents: .TouchUpInside)
return dynamicView
}
Then call your function from FirstViewController:
popupViewBeforeOrderCoupon = CustomView.createPopupViewWithList(self)