Home > Blockchain >  How to move function with button sender to a separate file?
How to move function with button sender to a separate file?

Time:06-02

I have a difficulty in a simple task. I googled this topic, but other examples are complicated by additional syntax that I don't understand yet. Can you help me to solve it or give link if there is already was similar topic.

I need to move the function responsible for selecting the button to a separate file, because if the number of buttons increases, it will turn into a large sheet. So made a function in separate swiftfile, but naturally the new file does not know about any buttons in viewController and can't find it in scope. Also If i’m not mistaken i need give Bool and return String.

How can I transfer a function with button sender to a separate file so that it returns non-optional text value back in the ViewController?

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var Label1: UILabel!
    @IBOutlet weak var BTC: UIButton!
    @IBOutlet weak var ETC: UIButton!
    @IBOutlet weak var LTC: UIButton!
    
    var choice = "Choose coin"
    var coinType = getCoinType()
    
    @IBAction func optionSelected(_ sender: UIButton) {
        BTC.isSelected = false
        ETC.isSelected = false
        LTC.isSelected = false
        sender.isSelected = true
        
        if BTC.isSelected == true{
            ETC.isSelected = false
            LTC.isSelected = false
            choice = "BTC"
            Label1.text = choice
        }else if ETC.isSelected == true{
            BTC.isSelected = false
            LTC.isSelected = false
            choice = "ETC"
            Label1.text = choice
        }else if LTC.isSelected == true{
            ETC.isSelected = false
            LTC.isSelected = false
            choice = "LTC"
            Label1.text = choice
        }
    }
    
}

new file, i can't understand how to get sender from buttons here

import Foundation

func getCoinType() -> String{
    var choice: String
    // my if else function
    return choice
}

P.S. In general, is it possible to make it easier, without using UIPickerView?

CodePudding user response:

On how to add functions in different swift file you have a few options. This a simple one: Create a new swift file and name it ViewController Extenstions.swift (you can use any name though). Then add an extension to your ViewController class and add your function like this:

extension ViewController {

    func getCoinType() -> String {
        var choice: String
        // my if else function
        return choice
    }

}

You can add as many functions as you need and in different files (all extensions to ViewController but of course with different file names).

PS, in your optionSelected function, you are setting:

BTC.isSelected = false
ETC.isSelected = false
LTC.isSelected = false

I don't know what you're trying to achieve, but by doing so, those if-else will never be executed since your are setting the three buttons as not-selected!

UPDATE: For your button selection problem, you can do this:

1- Add tags to your buttons You can do it inside viewDidLoad method. This way you can differentiate between different buttons.

override func viewDidLoad() {
    super.viewDidLoad()
    
    BTC.tag = 0
    ETC.tag = 1
    LTC.tag = 2

}

2- Connect the action of buttons Although your three buttons can have three different handler functions, it's easier to connect all of them to a single handler. However, we can know which button is tapped based on the value of the tag we assign in the previous step. So, connect all buttons to optionSelected(_ sender: UIButton) through Storyboard (because you have used Storyboard.

3- Rewrite getCoinType function

func getCoinType(tag: Int) -> String? {
    var choice: String?
    switch tag {
    case 0:
        choice = "BTC"

    case 1:
        choice = "ETC"
    
    case 2:
        choice = "LTC"
    
    default:
        choice = nil
    }
    return choice
}

Buttons' handler Now when a button is tapped we call getCoinType function with that button's tag as input argument. It will return the string and we assign it to the Label1:

@IBAction func optionSelected(_ sender: UIButton) {

    let choice = getCoinType(tag: sender.tag)
    Label1.text = choice
}

And you're done!

  • Related