The action in addTarget of my button does not work - swift

Xcode did not report errors but nothing was printed when I pressed the button.
I tried it with action:
#selector (print), action: #selector (self.print), action: #selector (ViewController.print)..etc..
Swift 4, Xcode 10.1
class SomeViewController:UIViewController {
var button:UIButton = UIButton()
func setupButton()
{
button = UIButton(frame: CGRect(x: 140, y: 140, width: 90, height: 40))
button.addTarget(self, action: #selector(print(-:)) , for: .touchUpOutside)
navigationController?.navigationBar.addSubview(button)
button.setTitle("Convert", for: .normal)
button.backgroundColor = .yellow
view.addSubview(button)
}
#objc func print(_sender: UIButton)
{
print("Stackoverflow")
}
override func viewDidLoad()
{
super.viewDidLoad()
view.addSubview(CollectionView)
CollectionView.frame = view.frame
setupButton()
}
}

You just have to change event type from .touchUpOutside to .touchUpInside
Here is events documentation

Change this ( You need touchUpInside instead of touchUpOutside )  
button.addTarget(self, action: #selector(print(_:)) , for: .touchUpOutside)
to
button.addTarget(self, action: #selector(printRes) , for: .touchUpInside)
#objc func printRes(_ sender: UIButton) { }
Also don't use print as button action name because it's a reserved method
Also to get the button click action set it's type
button = UIButton(type: .system)
button.frame = CGRect(x: 140, y: 140, width: 90, height: 40)

Related

swift can't find textfield in scope even when its in the same viewController

I need to print value of txtField after clicking the button, txtField is on the viewcotroller but xcode returns an error cannot find 'txtField' in scope but they are in the same view
override func viewDidLoad() {
super.viewDidLoad()
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)
var txtField: UITextField = UITextField()
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
Actually, no, it's not in scope. Your var txtField is inside the viewDidLoad function. No other function's code can see inside this function, so the variable is not in scope from within another function such as your buttonAction.
In general the rule that things inside a scope can see only things that at a higher level of scope. If var txtField appeared outside both viewDidLoad and buttonAction (i.e. an instance property), then code inside both would be able to see it.
It's an easy move to make:
var txtField: UITextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
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)
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
The error is that are in 2 different function, override or #objc doesn't change this simple thing. So move the txtField initialization outside them so can be readed or written by both.
var txtField: UITextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
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)
txtField.frame = CGRect(x: 50, y: 70, width: 200, height: 30)
txtField.backgroundColor = UIColor.gray
self.view.addSubview(txtField)
}
#objc func buttonAction(sender: UIButton!) {
print(txtField.text)
}
In your code txtField is declared in viewDidLoad on line
var txtField: UITextField = UITextField()
Therefore out of the scope of buttonAction.
To mitigate this place
txtField
out of scope as a instance variable

Swift/Xcode - No response from button in custom annotation view

I have created a custom annotation view, as described here: https://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/
It works fine and displays the correct data/info.
The problem seems to be that there is no respons to .touchUpInside from the added button.
let button = UIButton()
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
button.frame = CGRect.init(x: 16, y: 543, width: 268, height: 30)
button.backgroundColor = .yellow
calloutView.addSubview(button)
#objc func deleteFromAnnot(sender: UIButton)
{
print("Do something")
}
It looks to me like you have set your button target incorrectly.
Replace
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
With
button.addTarget(self, action: #selector(deleteFromAnnot(sender: )), for: .touchUpInside)

Swift 3: How can I add the buttons on the UIWeb View

I am doing a project and I want to add 6 buttons on the webview , but whenever I am trying to add a button, the app doesnot show this button.
Any suggestion, help really appreciated!
Thanks
Try This-
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: #selector(ratingButtonTapped), forControlEvents: .TouchUpInside)
self.profileVIew.addSubview(button)
func ratingButtonTapped(){
print("Button pressed")
}
I'm going to show you how to do this in code without using storyboards. Put his in your view controller class
var setupButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161, a: 256)
button.setTitle("Register", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
button.addTarget(self, action: #selector(handleRegister), for: .touchUpInside)
return button
}()
then in your viewDidLoad() function put
view.addSubview(setupButton)
myButton()
then outside of viewDidLoad() put
func myButton() {
setupButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
setupButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
setupButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
setupButton.heightAnchor.constraint(equalToConstant: 150).isActive = true }
func handleRegister {
#your action here
}

tvOS Button Action

I create a UIButton in tvOS via Swift
let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70)
self.view.addSubview(randomBtn)
But the action never get fired if I press the button, is there anything different in tvOS?
func click(sender: UIButton) {
print("click")
}
In tvOS for Button action UIControlEvents TouchUpInside will not call.
You have to use UIControlEvents PrimaryActionTriggered like below.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
Also Refer this link if you have any confusion
https://forums.developer.apple.com/thread/17925
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.PrimaryActionTriggered)
Solved it, its just a other UIControlEvents

How to create a UIButton programmatically

I am trying to build UIViews programmatically. How do I get a UIButton with an action function in Swift?
The following code does not get any action:
let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buttonPuzzle)
The following selector function is:
func buttonAction(sender: UIButton!) {
var btnsendtag: UIButton = sender
}
You're just missing which UIButton this is. To compensate for this, change its tag property.
Here is you answer:
let btn: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1 // change tag property
self.view.addSubview(btn) // add to view as subview
Swift 3.0
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle(title: "Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn)
Here is an example selector function:
func buttonAction(sender: UIButton!) {
var btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
//do anything here
}
}
Using a tag is a fragile solution. You have a view and you are creating and adding the button to that view, you just need to keep a reference to it: For example
In your class, keep a reference to the button
var customButton: UIButton!
Create the button and set the reference
let btn = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = .greenColor()
btn.setTitle("Click Me", forState: .Normal)
btn.addTarget(self, action: #selector(MyClass.buttonAction), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
customButton = btn
Test against this instance in the action function
func buttonAction(sender: UIButton!) {
guard sender == customButton else { return }
// Do anything you actually want to do here
}
You have to addSubview and tag on that btn.