Home > Net >  How to set first item selected by default when making a url request?
How to set first item selected by default when making a url request?

Time:09-20

I am using uiKit framework. I am using stack view and inside that are buttons, and data of that buttons is coming from server. I want that when i receive data, first button in stack view will be selected by defalut and the color changes. can anyone help please?

CodePudding user response:

// When you got data from URL request

if let _button = stackView.arrangedSubviews.first as? YourButtonClass {
    _button.isSelected = true // or your custom logic,..
}

CodePudding user response:

You need to get the first view that is a UIButton and then set his selected property to true

DispatchQueue.main.asyncAfter(deadline: .now()   1) { [weak self] in
        if let button = self?.stackview.arrangedSubviews.first(where: { $0 is UIButton }) as? UIButton {
            button.isSelected = true
    }
}
  • Related