@ About managing IBOutlet and actions when multiple buttons are tied to View

Asked 1 years ago, Updated 1 years ago, 52 views

I have a question about how to manage UIButton's actions.
I may not be able to ask you something, but I would appreciate it if someone could reply.

@IBOutlet weak var button_0:UIButton!
@IBOutlet weak var button_1—UIButton!
@IBOutlet weak var button_2:UIButton!
@IBOutlet weak var button_3:UIButton!
// ... and more than one.


Connecting and multiple UIButton to ViewController, etc. If you want to set the action for each button

 var buttons: UIButton = [ ]

ViewDidLoad, a ViewController that prepares and manages arrays, etc.

buttons.append(button_0)
buttons.append(button_1)
buttons.append (button_2)
// ... last for a few minutes or less
for in 0..<buttons.count {
    buttons[i].tag=i
    buttons[i].addTarget(self, action:#selector(ToolView.action(_:), forControlEvents: .TouchUpInside)
}

I don't think it's very good to describe as monotonous, but is there any good way?
After all, is it better to make the installation of multiple parts from the code?

*I'm going to use ViewController action like this...

@objc private function (sender:UIButton) {
    switch sender.tag {
    case0:
        print("button_0")
    case1:
        print("button_1")
    case2:
        print("button_2")
    // ... last for a few minutes
    default —break
    }
}

Thanks to you, it has been solved.
Quesera2, it was very easy to understand with images.Thank you.
Le Pered'OO also returned it late at night that day, which was helpful.Thank you.

It may have been a question that came up when I looked it up, but it was good that I was able to find out for the first time.
@IBOutlet Collection article
Starting Application Experience,
Stack overflow

swift ios storyboard

2022-09-30 21:19

2 Answers

When linking the Storyboard to the code @IBOutlet, you can specify Outlet Collection to associate multiple Views in an array.

Outlet Collection

However, in this case, it would be faster to link multiple buttons directly to one @IBAction.

Enter a description of the image here

@IBOutlet must be 1:1 while @IBAction can be tied 1:n.sender.tag works well by identifying buttons.

@IBAction function (sender:UIButton) {
    switch sender.tag {
    case0:
        print("button_0")
    case1:
        print("button_1")
    case2:
        print("button_2")
    // ... last for a few minutes
    default —break
    }
}


2022-09-30 21:19

In Swift,

@IBOutlet var buttons: UIButton = [ ]

You can combine multiple UIButton into a single property by declaring the @IBOutlet properties in an array as shown in .You do not need to declare individual button_0...as in your example to make it your own button.append(button_0).

I think it's easy to put the editor in assistant editor mode and connect (repeat) the black pochi in front of @IBOutlet on the code side to UIButton on the interface builder side.

If you try, you'll soon find out that it's not a big deal, so please try it.


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.