0

In my custom view I’ve created one button. To add action for that button which is feasible solution Creating Delegate method or Declaring UIButton as global variable and addTaget. Suggest me please.

Karthikeyan Bose
  • 1,244
  • 3
  • 17
  • 26

2 Answers2

1

I'd highly recommend not using global vars as much as possible. Global data (constants) are good, but global variables are high maintenance since anyone can modify it.

There are 2 cases why you may want a global variable:

  1. It has to trigger different actions in multiple unrelated submodules. What I'd recommend here is to create the button and target in a class/function, and send a notification in the target. Make the different subsystems register to the notification and take appropriate action
  2. You just have one class to take an action item, but it's convenient to not have delegates and use global vars. I request you to prefer design to convenience.
7to4
  • 198
  • 1
  • 8
1

Please don't use global variables. You have two option for same:

  1. If you are using custom class then you have to use delegate methods
  2. Another option is that: You can create a method with UIButton return type and use this method to create action method on the button in any class.

Note: I'd highly recommend you that you have to use delegate methods.

Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26