How to not add UIAlertAction to UIActionSheet if it has already been added

53 Views Asked by At

I'm adding a UIAlertAction with the following code:

    @IBAction func doneButtonTapped(_ sender: Any) {
        
        self.present(lastInstructionAlert, animated: true, completion: nil)
        
        lastInstructionAlert.addAction(UIAlertAction(title: "Back", style: .default, handler: { (action) in
            print("Go Back")
            return
        }))
        
        lastInstructionAlert.addAction(UIAlertAction(title: "Home", style: .default, handler: { (action) in
            print("Go Home")
            self.present(self.mealPlanViewController, animated: true, completion: nil)
        }))
        
    }

The issue is that when the user presses "Back" and then "Done" again, it adds the UIAlertActions again resulting in duplicates.

enter image description here

How can I prevent those UIAlertActions from being added again if they were already added before?

2

There are 2 best solutions below

0
Shehata Gamal On BEST ANSWER

As you make lastInstructionAlert an instance variable every time you click the action doneButtonTapped new alert actions are added , so make it local

@IBAction func doneButtonTapped(_ sender: Any) {
    
   let lastInstructionAlert = UIAlertController(title: "Well done,chef!", message: "It's time to eat", preferredStyle: .alert)

    lastInstructionAlert.addAction(UIAlertAction(title: "Back", style: .default, handler: { (action) in
        print("Go Back")
        return
    }))
    
    lastInstructionAlert.addAction(UIAlertAction(title: "Home", style: .default, handler: { (action) in
        print("Go Home")
        self.present(self.mealPlanViewController, animated: true, completion: nil)
    }))
     
    self.present(lastInstructionAlert, animated: true, completion: nil)
}

Or move adding the alert actions say to viewDidLoad

0
Shehryar Ahmad Rajput On

The issue lies in the coding of the action sheet, please follow the function I have made below and use it in your projects, you can add more buttons if you wish.

I'd recommend making a new project, and putting this func (as it is) in the viewcontrolling and calling it in viewDidload to see how it works. You can then adjust/alter the code for your main project.

/**
 Simple Action Sheet
 - Show action sheet with title and alert message and actions
 */
func showSimpleActionSheet(controller: UIViewController) {
    let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Approve", style: .default, handler: { (_) in
        print("User click Approve button")
    }))

    alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { (_) in
        print("User click Edit button")
    }))

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (_) in
        print("User click Delete button")
    }))

    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { (_) in
        print("User click Dismiss button")
    }))

    self.present(alert, animated: true, completion: {
        print("completion block")
    })
}

This is from an older version of swift in my project. If you paste it into your project, xcode will automatically fix it when the warnings pop up. You'll just need to click them and it'll be fixed.