0

I have created an alert Where a mybutton have to be hidden while the activity indicator has to be shown. but it didn't work. Here is the code:

private func showAlert() {
    let alert = UIAlertController(title: "aaaaaa", message: ccccccccc, preferredStyle: .alert)
    let attributedString = NSAttributedString(string: "aaaaa", attributes: [.font: UIFont.boldSystemFont(ofSize: 18), //your font here
         .foregroundColor : Theme.bleu!])
    alert.setValue(attributedString, forKey: "attributedTitle")
    alert.addAction(UIAlertAction(title: annuler, style: .cancel))
    alert.addAction(UIAlertAction(title: optimiserLesMicrosUniquement, style: .default, handler: { (action) in
        self.myButton.isHidden = true // Here begins the problem
        self.activityIndicator.isHidden = false
        let menuOptimiser = functionNumberX()
        self.activityIndicator.isHidden = true // Here is the problem
        self.myButton.isHidden = false
        let reponse = menuOptimiser.0
        if reponse == false  {
            self.showAlerteMenuOptimal()
        } else {
            if menuOptimiser.1 > 0 {
                self.presentAlerteDuGain(gain: menuOptimiser.1, menu: menuOptimiser.2)
            } else {
                self.presentAlerteOptimisationSansGainDeCalories(calorie: abs((round(menuOptimiser.1))), menu: menuOptimiser.2)
            }
        }
        self.gestionDeLaNotationDuMenuEnCours()
    }))
    present(alert, animated: true)
}

When i execute it, mybutton never disappears, and activity indicator never appears.

i don't know how to do that.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Wahib
  • 93
  • 1
  • 8

2 Answers2

0

try removing, these two lines , since you are opposite at almost the same time .

self.activityIndicator.isHidden = true // Here is the problem
self.myButton.isHidden = false
YodagamaHeshan
  • 4,996
  • 2
  • 26
  • 36
  • 1
    i already tried to put these two in dispatchqueue.async, and have tried dispatchqueue.sync too. but it didn't work – Wahib Nov 22 '20 at 04:45
  • I am asking you to remove above two lines . since you are hiding the myButton in a above line in your code. – YodagamaHeshan Nov 22 '20 at 04:47
  • just remove above two line I have added in this answer . – YodagamaHeshan Nov 22 '20 at 04:47
  • 1
    i can't remove them. i need to show them in the end of the execution of functionNumberX() – Wahib Nov 22 '20 at 04:56
  • that is where the problem is , it return in a moment after executing , if you want to do hide indicator after executing it you can pass above two line as a completion to your functionNumberX. – YodagamaHeshan Nov 22 '20 at 05:00
  • if you can add your functionNumberX() too , I can fix it here – YodagamaHeshan Nov 22 '20 at 05:00
  • @Monica check this post how to add an activity indicator https://stackoverflow.com/questions/28785715/how-to-display-an-activity-indicator-with-text-on-ios-8-with-swift/28893660#28893660 – Leo Dabus Nov 22 '20 at 05:01
  • 1
    @Yodagam, please if you can show me how to put it there, because i have tried, but i failed. the function is very simple, – Wahib Nov 22 '20 at 05:08
  • 1
    @LeoDabus I already read your post, but i didn't found what i need. I already add an activity indicator in other case, and it works. but i failed in this one – Wahib Nov 22 '20 at 05:10
  • please add the implementation pf functionNumberX() , then i will post the answer. thats the easy way. – YodagamaHeshan Nov 22 '20 at 05:12
0

I just found a solution. Here is the code

var menuOptimiser : (Bool, Float, [[AlimentObject]])!

private func showAlert() {
    let alert = UIAlertController(title: "aaaaaa", message: ccccccccc, preferredStyle: .alert)
    let attributedString = NSAttributedString(string: "aaaaa", attributes: [.font: UIFont.boldSystemFont(ofSize: 18), //your font here
         .foregroundColor : Theme.bleu!])
    alert.setValue(attributedString, forKey: "attributedTitle")
    alert.addAction(UIAlertAction(title: annuler, style: .cancel))
    alert.addAction(UIAlertAction(title: optimiserLesMicrosUniquement, style: .default, handler: { (action) in
        self.myButton.isHidden = true // Here begins the problem
        self.activityIndicator.isHidden = false

   DispatchQueue.main.asyncAfter(deadline: .now()) {
        let menuOptimiser = functionNumberX()
        self.activityIndicator.isHidden = true // Here is the problem
        self.myButton.isHidden = false
        let reponse = menuOptimiser.0
        if reponse == false  {
            self.showAlerteMenuOptimal()
        } else {
            if menuOptimiser.1 > 0 {
                self.presentAlerteDuGain(gain: menuOptimiser.1, menu: menuOptimiser.2)
            } else {
                self.presentAlerteOptimisationSansGainDeCalories(calorie: abs((round(menuOptimiser.1))), menu: menuOptimiser.2)
            }
        }
   }
        self.gestionDeLaNotationDuMenuEnCours()
    }))
    present(alert, animated: true)
}

Wahib
  • 93
  • 1
  • 8
  • Thats what I meant `DispatchQueue.main.async` no need to add `After(deadline: .now())` – Leo Dabus Nov 22 '20 at 05:55
  • 1
    @LeoDabus The first dispatchqueue that i tested is when i put let menuOptimiser = functionNumberX() in it. bu i couldn't work. i've had to put everything in it – Wahib Nov 22 '20 at 06:00