xcode 警告:UIAlertController 已经呈现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38262593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:01:38  来源:igfitidea点击:

Warning: UIAlertController is already presenting

iosxcodeswiftwarningsuialertcontroller

提问by CatiaV5

I am a novice in swift and XCode so please forgive me the probably really messy code! When running my code I get the following warning: Warning: Attempt to present on which is already presenting

我是 swift 和 XCode 的新手,所以请原谅我可能真的很乱的代码!运行我的代码时,我收到以下警告:警告:尝试呈现已经呈现的

I have already checked the forum and found a post regarding that issue....I applied the help suggested as in:

我已经检查了论坛并找到了有关该问题的帖子......我应用了建议的帮助,如下所示:

if presentedViewController !== nil {
     self.presentViewController(alertController, animated: true, completion: nil)
} else{
     self.dismissViewControllerAnimated(false, completion: nil)
     self.presentViewController(alertController, animated: true, completion: nil)
}

However, it still gives me the same warning....So I guess I must have done something wrong there. It would be great if someone could help me out please :) Also if there any suggestion as to how I can "clean" up my code are appreciated :)

但是,它仍然给了我同样的警告......所以我想我一定在那里做错了什么。如果有人可以帮助我,那就太好了:) 另外,如果有关于如何“清理”我的代码的任何建议,我将不胜感激:)

import UIKit

class ViewController: UIViewController {

@IBOutlet var Label: UILabel!

@IBOutlet var ukFlag: UITextView!

@IBOutlet var movie: UITextView!

@IBOutlet var piano: UITextView!

@IBOutlet var book: UITextView!

@IBOutlet var dance: UITextView!

@IBOutlet var paint: UITextView!

@IBOutlet var row: UITextView!

@IBOutlet var envelope: UITextView!

@IBOutlet var dragon: UITextView!

@IBOutlet var instagram: UITextView!


func londonPuzzle(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
      self.ifCollided()


}


func misleadOne(sender: UIPanGestureRecognizer) {

    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func bestival(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
      self.ifCollided()

    }

func rosieProject(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func misleadTwo(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func misleadThree(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func misleadFour(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}
func voucher(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func welsh(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func hulk(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()

}

func ShowAlertWrong(){

    let alertController = UIAlertController(title: "Sorry", message:   " Guess again!", preferredStyle: .Alert)

    let okAction = UIAlertAction( title: "Ok", style:   UIAlertActionStyle.Default) {
        UIAlertAction in

        self.ukFlag.center = CGPointMake(150, 225)
        self.movie.center = CGPointMake(150, 225)
        self.piano.center = CGPointMake(150, 225)
        self.dance.center = CGPointMake(150, 225)
        self.paint.center = CGPointMake(150, 225)
        self.row.center = CGPointMake(150, 225)
        self.envelope.center = CGPointMake(150, 225)
        self.dragon.center = CGPointMake(150, 225)
        self.instagram.center = CGPointMake(150, 225)
    }

    let resetAction = UIAlertAction( title: "Reset", style: UIAlertActionStyle.Default) {
        UIALertAction in

        self.ukFlag.center = CGPointMake(150, 225)
        self.movie.center = CGPointMake(150, 225)
        self.piano.center = CGPointMake(150, 225)
        self.dance.center = CGPointMake(150, 225)
        self.paint.center = CGPointMake(150, 225)
        self.row.center = CGPointMake(150, 225)
        self.envelope.center = CGPointMake(150, 225)
        self.dragon.center = CGPointMake(150, 225)
        self.instagram.center = CGPointMake(150, 225)

    }

    alertController.addAction(okAction)
    alertController.addAction(resetAction)
    self.presentViewController(alertController, animated: true, completion: nil)

}




func ShowAlertWin(){



    let alertController = UIAlertController(title: "Congrats", message: " You are correct!", preferredStyle: .Alert)

    let okAction = UIAlertAction( title: "Ok", style: UIAlertActionStyle.Default) {
        UIAlertAction in

        self.book.center = CGPointMake(150, 150)


    }

  /*  let resetAction = UIAlertAction( title: "Reset and hope the mood has changed", style: UIAlertActionStyle.Default) {
        UIALertAction in

        self.Angry.center = CGPointMake(150, 150)
    }*/

    alertController.addAction(okAction)
   // alertController.addAction(resetAction)
    if presentedViewController !== nil {
        self.presentViewController(alertController, animated: true, completion: nil)

    } else{
        self.dismissViewControllerAnimated(false, completion: nil)
        self.presentViewController(alertController, animated: true, completion: nil)
    }



}






@IBAction func ifCollided(){

if CGRectIntersectsRect(Label.frame, ukFlag.frame) {
    ShowAlertWrong()
}
    else if CGRectIntersectsRect(Label.frame, movie.frame){
           ShowAlertWrong()
        } else if CGRectIntersectsRect(Label.frame, piano.frame){
                ShowAlertWrong()
        }  else if CGRectIntersectsRect(Label.frame, dance.frame){
            ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, paint.frame){
    ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, row.frame){
    ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, envelope.frame){
    ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, dragon.frame){
    ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, instagram.frame){
    ShowAlertWrong()
} else if CGRectIntersectsRect(Label.frame, book.frame){
    ShowAlertWin()
}


}




override func viewDidLoad() {
    super.viewDidLoad()
    ukFlag.addGestureRecognizer(UIPanGestureRecognizer(target: self,      action: #selector(self.londonPuzzle(_:))))
    movie.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.misleadOne(_:))))
    piano.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.bestival(_:))))
    book.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.rosieProject(_:))))
    dance.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.misleadTwo(_:))))
    paint.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.misleadThree(_:))))
    row.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.misleadFour(_:))))
    envelope.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.voucher(_:))))
    dragon.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.welsh(_:))))
    instagram.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.hulk(_:))))


}

}

回答by Nirav D

You need to change your code like this, present the second alertController on completion of dismiss. Also change your if condition with my one.

您需要像这样更改代码,在完成关闭时显示第二个 alertController。也用我的条件改变你的 if 条件。

if presentedViewController == nil {
     self.presentViewController(alertController, animated: true, completion: nil)
} else{
      self.dismissViewControllerAnimated(false) { () -> Void in
           self.presentViewController(alertController, animated: true, completion: nil)
      }
}

Hope this will help you.

希望这会帮助你。

回答by Vimal Saifudin

Try this

尝试这个

if let presented = self.presentedViewController {
     presented.removeFromParentViewController()
  }

if presentedViewController == nil {
     self.present(alert, animated: true, completion: nil)
  }

回答by Long.h.nguyen

You can present another UIAlertController or anything else you want from current presented UIAlertController. Create a helper func to get top visible viewcontroller then make a present call on that

你可以从当前呈现的 UIAlertController 中呈现另一个 UIAlertController 或任何你想要的东西。创建一个辅助函数以获取顶部可见的视图控制器,然后对其进行当前调用

func topVisibleViewController(viewController: UIViewController) -> UIViewController? {
    let viewController = viewController ?? UIApplication.shared.keyWindow?.rootViewController

    if let navigationController = viewController as? UINavigationController, !navigationController.viewControllers.isEmpty {
        return visibleViewController(navigationController.viewControllers.last)
    }
    else if let tabBarController = viewController as? UITabBarController,
        let selectedController = tabBarController.selectedViewController {
        return visibleViewController(selectedController)
    }
    else if let presentedController = viewController?.presentedViewController {
        return visibleViewController(presentedController)
    }

    return viewController
}

回答by Zouhair Sassi

Try this:

尝试这个:

   [self dismissViewControllerAnimated:YES completion:nil];
    UIAlertController *alertResync = [UIAlertController alertControllerWithTitle:@"Warning"
                                                                         message:@"Warning Warning"
                                                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *softResyncButton = [UIAlertAction actionWithTitle:@"No, keep Some"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * _Nonnull action) {

                                                             }];

        UIAlertAction *hardResyncButton = [UIAlertAction actionWithTitle:@"Yes"
                                                                   style:UIAlertActionStyleDestructive
                                                                 handler:^(UIAlertAction * _Nonnull action) {
                                                                     [self testFunc];
                                                                 }];
        [alertResync addAction:softResyncButton];
        [alertResync addAction:hardResyncButton];
        [self presentViewController:alertResync animated:YES completion:nil];

回答by Manganese

I appreciate the dismissal of Alerts as mentioned in all of the answers. But I personally would never prefer dismissing something I didn't want to conjure up in the first place. Hence, if we could just look at why multiple alerts are generated in the first place, it would be awesome.

我很欣赏所有答案中提到的警报的解除。但我个人永远不会喜欢驳回我一开始就不想想到的事情。因此,如果我们可以首先看看为什么会生成多个警报,那就太棒了。

Using UIGestureRecogniserfor generating UIAlert is tricky. Gestures send messages in all their underlying states. For example: if below is my code:

使用UIGestureRecogniser产生UIAlert是棘手的。手势以其所有底层状态发送消息。例如:如果下面是我的代码:

// Attaching LongPress to a button
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(deleteSelectedProfile(_:)))

// Method to generate appropriate alert
@objc func deleteSelectedProfile(_ sender: UILongPressGestureRecognizer) {
    let alert = UIAlertController(title: "Delete Profile", message: "Word!" , preferredStyle: .alert)
    let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { (action) in
        // Code
    }
    alert.addAction(deleteAction)    
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alert.addAction(cancelAction)
    present(alert, animated: true, completion: nil)
}

UIAlertis be called upon for various states of the Gesture, starting with .began, till .ended. Hence, if clean code is what I need, I would never want to dismiss any of the alerts created at each of the states in the Gesture, rather will generate only one UIAlert. Meaning, I would simply use the State as my condition.

手势的各种状态都会调用UIAlert,从.began, 到.ended。因此,如果我需要干净的代码,我绝不会取消在 Gesture 中的每个状态创建的任何警报,而是只生成一个 UIAlert。意思是,我会简单地使用状态作为我的条件。

Hence, here is what I will change:

因此,这是我将要更改的内容:

@objc func deleteSelectedProfile(_ sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        let alert = UIAlertController(title: "Delete Profile", message: "Word!" , preferredStyle: .alert)
        let deleteAction = ...
        alert.addAction(deleteAction)    
        let cancelAction = ...
        alert.addAction(cancelAction)
        present(alert, animated: true, completion: nil)
    }
}

This way I will be generating one alert and one alert only when required.

这样,我将仅在需要时生成一个警报和一个警报。

回答by Prakhar Prakash Bhardwaj

This is how I debug this error in Swift 4.2:

这是我在 Swift 4.2 中调试此错误的方式:

     let alert = UIAlertController(title: "Oops!", message: "No Internet Connection", preferredStyle: .alert)
        let alertAction = UIAlertAction(title: "OK", style: .default)
        alert.addAction(alertAction)
        if let presented = self.presentedViewController {
            presented.removeFromParent()
        }

        if presentedViewController == nil {
            self.present(alert, animated: true, completion: nil)
        }       
      }