web-dev-qa-db-ja.com

警告:UIAlertControllerは既に表示されています

私はSwiftとXCodeの初心者なので、おそらく本当に厄介なコードを許してください!コードを実行すると、次の警告が表示されます。

私はすでにフォーラムをチェックし、その問題に関する投稿を見つけました...私は次のように提案されたヘルプを適用しました:

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

しかし、それでも同じ警告が表示されます....そこで、何か間違ったことをしたのではないでしょうか。誰かが私を助けてくれれば幸いです:)また、コードを「クリーンアップ」する方法についての提案があれば幸いです:)

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(_:))))


}

}
9
CatiaV5

このようにコードを変更し、閉じるのが完了したら2番目の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)
      }
}

これがお役に立てば幸いです。

21
Nirav D

これを試して

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

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

これは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)
        }       
      }

すべての回答に記載されているように、アラートの却下に感謝します。しかし、私は個人的に、最初に思い起こさせたくないものを却下することは決して望みません。したがって、最初に複数のアラートが生成される理由を確認できれば、それはすばらしいことです。

UIAlertの生成にUIGestureRecogniserを使用するのは難しいです。ジェスチャーは、すべての基本的な状態でメッセージを送信します。例:以下が私のコードの場合:

// 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)
}

IAlertは、.beganから.endedまで、ジェスチャーのさまざまな状態で呼び出されます。したがって、クリーンなコードが必要な場合は、ジェスチャーの各状態で作成されたアラートをすべて消したくはなく、UIAlertを1つだけ生成します。つまり、状態を状態として使用するだけです。

したがって、これが私が変更するものです:

@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)
    }
}

このようにして、必要な場合にのみ1つのアラートと1つのアラートを生成します。

0
Manganese

別の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
}
0
Long.h.nguyen

これを試して:

   [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];
0
Zouhair Sassi