0

I have this design in storyboard, it's all button which i stack simultaneously. But i have difficulty when i try to connect it to code, seems so repeated.

enter image description here

I have tried using regular drag-and-drop to connect all the buttons, but it seems so long and repeated. is there best practice to connect and make one listener that could be listened by all of the buttons? using for-loop may be.

rony
  • 500
  • 1
  • 6
  • 21

2 Answers2

2

And u can just link them in storyboard.

Objective-C

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray * buttons; 

Swift 2.2

@IBOutlet var collectionOfButtons = [UIButton]()
override func viewDidLoad() {
    super.viewDidLoad()
    collectionOfButtons.forEach {
        $0.addTarget(self, action: #selector(someFunc(_:)), forControlEvents: .TouchUpInside)
    }
}

Or much simpler way:

override func viewDidLoad() {
    super.viewDidLoad()
    for view in view.subviews {
        (view as? UIButton)?.addTarget(self, action: #selector(someFunc(_:)), forControlEvents: .TouchUpInside)
    }
}
func someFunc(sender: UIButton?) {}

Swift 3.0

override func viewDidLoad() {
    super.viewDidLoad()
    view.subviews.forEach {
        ($0 as? UIButton)?.addTarget(self, action: #selector(someFunc(_:)), for: .touchUpInside)
    }
}
func someFunc(_ sender: UIButton?) {}
Dmytro Shvetsov
  • 946
  • 10
  • 13
  • and how to add actions? where? and how? thanks Dmytro – rony Oct 01 '16 at 16:22
  • @chronycles It's really pain to create all this buttons in storyboard – Dmytro Shvetsov Oct 01 '16 at 16:36
  • Exactly! that's why i'm so confused. i'm using swift 3 and using button not UiView, do i have to write the code as your code too? using view.subviews.forEach? or do i have to write like this seats.forEach { (seat) in seat.addTarget(self, action: #selector(handleButton), forControlEvents: .TouchUpInside) } – rony Oct 01 '16 at 16:43
  • If you insert buttons in main View, then like my code. Because UIButton Is subview of UIVIew. And all your buttons, and views are stored in self.view.subviews. And What is seats ? if seats is collections of all buttons, yes seats.forEach – Dmytro Shvetsov Oct 01 '16 at 16:47
  • seats is collection of all buttons. and .touchUpInside is a listener when user touch the button, what if i want to use another listener, for example from server. When the server send data to specific button, the button color turns red or something. What should i do? Thanks – rony Oct 01 '16 at 16:51
  • for what u say u create another func. When the server send data to specific button, u call this func just self.someFunc(seats[5]). – Dmytro Shvetsov Oct 01 '16 at 16:52
  • i have this error: reason: '-[UIButton copyWithZone:]: unrecognized selector sent to instance 0x7fd1424922f0' – rony Oct 01 '16 at 17:09
  • I'am sorry, first time see this. Maybe it will help you http://stackoverflow.com/questions/10784207/uilabel-copywithzone-unrecognized-selector-sent-to-instance – Dmytro Shvetsov Oct 01 '16 at 17:35
0

you can use switch instead for loop here we go:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button  = (Button)findViewById(R.id.btn1);
    Button button2  = (Button)findViewById(R.id.btn2);
    ..
    ....Another buttons
    .....

    button.setOnClickListener(onClickButtons);
    button2.setOnClickListener(onClickButtons);

}

View.OnClickListener onClickButtons = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1: // do smth 
                 break;
            case R.id.bt2: //do another thing
                break;
            ..
            ...
            ....
            default :
                break;
        }
    }
};
dara
  • 763
  • 7
  • 14