-1

I want to add another events when the button is clicked. i use addTarget to observe the button when is clicked and call a method name showModal. see the code below.

headerView.addTaskButton.addTarget(self, action: #selector(showModal), for: .touchUpInside)

and i want to add another action when that button is pressed. but i don’t know where.

self.todoListViewModel.updateMode(.edit)

is there any way to do it with addTarget? like adding closure and stuff?

  • I want only addTaskButton to update the mode. if i added self.todoListViewModel.updateMode(.edit) in showModal method, everything calling that method will be updated the mode as .edit

so i don’t want to put that in showModal method. is there anyway?

Doyeon
  • 7
  • 7
  • Your question is confusing... Do you have **two** buttons or only **one**? If only one, do you want to execute `self.todoListViewModel.updateMode(.edit)` when the button is *pressed* (`.touchDownInside`), and `showModal()` when the button is released (`.touchUpInside`)? – DonMag Jan 04 '21 at 14:25

2 Answers2

1

You can have 2 actions declared :

func commonAction(_ sender: UIButton) {
    // code for all buttons
}
func showModal(_ sender: UIButton) {

    self.todoListViewModel.updateMode(.edit)
    self.commonAction(sender)
}

Another option : set the tag of the button to know if you must do the action :

func commonActionOrModal(_ sender: UIButton) {
    If sender.tag == 1 {
        self.todoListViewModel.updateMode(.edit)
    }
    // code for all buttons
}
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
0

You don't need to add another action, just place your code in "showModal" function

func showModal(_ sender: UIButton) {
   
   self.todoListViewModel.updateMode(.edit)

   // your remaining code

}
  • i don’t want to updateMode for another calling action. like i only want that addTaskButton to only update the mode. i try to get #func as a parameter to know which calls the method, but error occured. – Doyeon Jan 04 '21 at 08:18