How to call a function inside a function in Swift - ios

#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

Related

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 to use function to call button instead of pressing it (swift3)

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)

Swift - pass the same parameter from one function to the other

I have two functions in swift 2 code.
func myfunctionA (sender: UIButton!){
// some code
myfunctionB (sender: UIButton!) }
The other function is func myfunctionB.
I call myfunctionB from inside of myfunctionA.
I need to know how to pass the same sender parameter from myfunctionA to myfunctionB.
func myfunctionA (sender: UIButton!){
myfunctionB(sender);
}
should work

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

Resources