How to use function to call button instead of pressing it (swift3) - ios

I want to be able to call function start within function use without hitting the action button for start. I know simple thing to do is just put print("a") in use. But I am using this as a simple example because I have a more complex problem in mind.
#IBAction func start(_ sender: Any) {
print("a")}
fun use() {
}
viewdidload() {
use()
}

Consider refactoring your functions. Instead of putting the button action code directly inside of the #IBAction function, put it in a separate function. This way, you can call this code from multiple places.
Here is one solution:
#IBAction func start(_ sender: Any) {
startAction()
}
func startAction() {
print("a")
}
func use() {
startAction()
// anything else
}
override func viewDidLoad() {
use()
}

Create an IBOutlet for your button:
#IBOutlet weak var button: UIButton!
Then simply, use this code to trigger its action:
button.sendActions(for: .touchUpInside)

Related

IBAction Buttons have same functionality. Is it possible to refactor?

I'm learning Swift and I'm making a tic-tac-toe game now. Here is the image of the game and everything works fine so far.image of tic-tac-toe. However, I was wondering if there is a way to refactor the code or not.
I added a plate image to each button (so you can see 3 X 3 buttons), so when a user taps the button, the plate image becomes either an apple image or a pineapple image.
In the code below, I made IBAction for each button (i.e. func plate1Pressed()), but every IBAction execute the same function, which is changePlateImage(plate: sender). So far, I only have 9 buttons in total, so I can just make IBAction 9 times and put changePlateImage(plate: sender) in them, however, I was thinking if I had to make more square games like Reversi, I had to make 8 X 8 IBActions, which is kind of terrifying...
So is there any way to refactor my code? instead of adding IBActions 9 times and put the same function in them?
import UIKit
class GameScreenViewController: UIViewController {
var isPlayer1 = true
override func viewDidLoad() {
super.viewDidLoad()
}
// when player 1 taped a button, change isPlayer1 to flase
func displayHandPointer () {
// some codes here...
}
func chnagePlayerTurn () {
// some codes here...
}
func changePlateImage (plate: UIButton) {
let fruitImage = isPlayer1 ? K.Image.apple : K.Image.pineapple
plate.setImage(UIImage(named: fruitImage), for: .normal)
chnagePlayerTurn()
displayHandPointer()
}
//MARK: - IBA actions for board game
#IBAction func plate1Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate2Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate3Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate4Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate5Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate6Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate7Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate8Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
#IBAction func plate9Pressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
// and codes go on...
}
You don't need to create 9 IBAction to do that. sender is passed in as a parameter when it's tapped, and it differs each time when different plate is tapped.
So only one IBAction is needed to call changePlateImage.
Do the following:
Connect plate1 to plate9 to platesPressed IBAction func
#IBAction func platesPressed(_ sender: UIButton) {
changePlateImage(plate: sender)
}
func changePlateImage (plate: UIButton) {
// keep the same code
}
You can use the same IBAction for all UIButtons and then use tags to know which buttons you are pressing.
#IBAction func platePressed(_ sender: Any) {
let button = sender as! UIButton
if button.tag == 1 {
// do something
}
}
View tags can be set inside the interface builder

why i have to use print((sender as AnyObject).currentTitle!!) to print title and print(sender.currentTitle) not work?

Why when I try to print button title i used print(sender.currentTitel) and isn't working.
And this in below it is work:
print((sender as AnyObject).currentTitle!!)
I assume you are in a IBAction function like this:
#IBAction func buttonTapped(_ sender: Any) {
// print here
}
This is due to the Any reference you declare when you create the IBAction. Two solution.
You can modify your IBAction like this:
#IBAction func buttonTapped(_ sender: UIButton) {
// print(sender.titleLabel?.text)
}
or test the sender conformance:
#IBAction func buttonTapped(_ sender: Any) {
if let button = sender as? UIButton {
// print(button.titleLabel?.text)
}
}
Solution 1 is better if your IBAction is only triggered by button(s)
Solution 2 may be an approach if your IBAction is used by multiple senders
Cheers

Call extension function IOS Swift

How can I call extension function, so that the button is encouraged to call the view.
I made a UiButton Extension with a function to animate a button, and everything works, but only if I call it from:
#IBAction func botonVuelta(_ sender: UIButton) {
sender.pulsarAnimacion()
}
but if you called it from viewDidLoad it doesn’t work:
override func viewDidLoad() {
super.viewDidLoad()
botones.layer.cornerRadius = 20
botones.pulsarAnimacion()
}
I’d appreciate it if you could give me a solution,
I thank you in advance
viewDidLoad is a very early place to animate a view , try inside
override func viewDidAppear(_ animated:bool) {
super.viewDidAppear(animated)
botones.pulsarAnimacion()
}

How do I call a button function when the button is not being pressed

I have an IBAction connected to a button, and I wanted to know if there is any way to run that function even if the button is not being pressed. This is what I tried...
Note: I am using swift for this project.
//I get an error when I type this code?
self.buttonPressed()
#IBAction func buttonPressed(sender: AnyObject) {
print("Called Action")
}
Make your sender argument optional and pass nil to ButtonPressed.
self.ButtonPressed( nil )
#IBAction func ButtonPressed( sender: AnyObject? ) {
println("Called Action")
}
One way would be to link your button up to its respective interface builder button and pass it into your function when you call it.
#IBOutlet weak var yourButton: UIButton!
self.buttonPressed(yourButton)
#IBAction func buttonPressed(sender: AnyObject) {
print("Called Action")
}
Alternatively, define your function like so, then you'll be able to call your method the same way as you did before:
#IBAction func buttonPressed(sender: AnyObject? = nil) {
print("Called Action")
}
// Call your method like this
self.buttonPressed()
// or this
self.buttonPressed(sender: nil)
Your ButtonPressed function needs an argument sender and yet you're not passing any when you call it. However, if you're doing this programatically, then you obviously don't have a sender.
Two ways to get around this:
Make the sender parameter an optional (AnyObject? instead of AnyObject) and invoke self.ButtonPress(nil). I just tried this and it works.
Put all the button press function in a separate function, e.g., performButtonPress(). Call it from inside the IBAction outlet, and if you want to press the button programatically, directly call performButtonPress().
Call a button action using the following format: Swift 3
#IBAction func buttonSelected(_ sender: UIButton) {
print("Butten clicked")
}
To call the button action:
let button = UIButton()
self.buttonSelected(button)
You need to use "self" as "sender", example:
self.buttonPressed (sender: self)
or a simpler version
buttonPressed (sender: self)
Both tested in Swift 3
lazy var Button : UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(title: "Button", style: .done, target: self, action: #selector(btnSelection))
return barButtonItem
}()
func btnSelection(){
// Code
}
// Now you can call self.btnSelection() whenever you want

How to call a function inside a function in Swift

#IBAction func first(sender: AnyObject) {
println("Hello World")
}
#IBAction func second(sender: Anyobject) {
// I need to call function first here.
}
I need to use one function inside another, because the sender type is Anyobject, I don't know how to call it.
Have you tried
func first(){
println("Hello World")
}
#IBAction func second(sender: AnyObject) {
first()
}
If you are sure about the type of control,it should be UIControl object likeUIButton,UISegmentControl etc, then change the sender to that type.
If both function's parameter are same you can do like
#IBAction func second(sender: UIButton) {//it sure that you are clikcing UIButton
self.first(sender)//if here also a button, else create the control and pass.
}
But why you want first function as #IBAction? If it is not attached to any control please make it as normal function

Resources