-1

How do I reference self from within a UIAlertAction?

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
        // self.title = String("HELLO")
        print("Handle Ok logic here")
    }))

I want the title of the UIAlertAction to change when it is clicked on (as well as other things). How do I reference myself in the command for what to do when clicked on?

How do I make it where clicking on a UIAlertAction doesn't close the alert action

TylerP
  • 9,600
  • 4
  • 39
  • 43
Marcus
  • 9,032
  • 11
  • 45
  • 84

2 Answers2

1

If you want to access a UIAlertAction object from its own handler, just use the provided action parameter passed to the action handler block.

However, you cannot change the title of an action after it has been created since the title parameter is readonly.

Also, however, there is little reason to even change the title anyway, as alerts are always dismissed upon tapping any alert action's button.

TylerP
  • 9,600
  • 4
  • 39
  • 43
0

UIAlertController's title property is read only. you can't assign anything. however, you can handle that to use this code.

var propTitle:String = "BYE" // it's property of class

func method(){
    refreshAlert.addAction(UIAlertAction(title: propTitle, style: .Default, handler: { (action: UIAlertAction!) in
        self.propTitle = "HELLO"
    }))
}