簡體   English   中英

如何使用協議關閉和顯示自定義彈出窗口(UIViewController)-SWIFT

[英]How to dismiss and present a custom popup (UIViewController) using protocols - SWIFT

我有一個單視圖應用程序。 我按下一個按鈕,然后出現一個自定義彈出窗口(UIViewController)(演示文稿:在當前上下文中)。 如果我在第一個彈出窗口上按下一個按鈕,我還會有第二個自定義彈出窗口(UIViewController)出現(演示:在當前上下文中)-該按鈕將被關閉,並在緊接着彈出第二個彈出窗口。 我的問題如下:我無法關閉第二個彈出窗口,而無法通過第二個彈出窗口上的按鈕立即顯示第一個彈出窗口。 我一直在使用協議/委托來使第一個彈出窗口出現,但無法使第二個彈出窗口消失並帶回第一個彈出窗口。 我評論了我在哪一行出現錯誤。

class HomePage: UIViewController, popUpDismissedDelegate, CreateHashTagDelegate{

    @IBAction func showFirtPopUp(_ sender: Any) {
        let firstPopUpVC = storyboard?.instantiateViewController(withIdentifier: 
        "firstPopUp") as! FirstPopUp
         firstPopUpVC.delegate = self
         present(firstPopUpVC, animated: true, completion: nil)
    }


    // CUSTOM PROTOCOL DELEGATE FUNCTION
    func popUpDimissed() {

    // PRESENT SECOND POPUP
    let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "secondPopUp") as! SecondPopUp
    present(secondPopUpVC, animated: true, completion: nil)
    }

    // PRESENT FIRST POPUP
    func popUpCreateHTagDismissed() {
        let firstPopUpVC = storyboard?.instantiateViewController(withIdentifier: "firstPopUp") as! FirstPopUp
    present(firstPopUpVC, animated: true, completion: nil)

    }



    override func viewDidLoad() {
    super.viewDidLoad()

        let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "createTag") as! SecondPopUp
        secondPopUpVC.createHTagDelegate = self
    }

}


protocol popUpDismissedDelegate {
    func popUpDimissed()

class FirstPopUp: UIViewController{

    var delegate: popUpDismissedDelegate!

    // DISMISS FIRST POPUP AND PRESENT SECOND POPUP ON TOP OF THE HOMEPAGE
    @IBAction func showFirtPopUp(_ sender: Any) {
        self.dismiss(animated: true) {
            self.delegate.popUpDimissed() // ** 2nd iteration
    }

}



protocol CreateHashTagDelegate {
    func popUpCreateHTagDismissed()
}

class SecondPopUp: UIViewController{

    var createHTagDelegate: CreateHashTagDelegate!

    // DISMISS SECOND POPUP AND PRESENT FIRST POPUP
    @IBAction func showFirstPopUp(_ sender: Any) {

        self.dismiss(animated: true, completion: {

        self.createHTagDelegate.popUpCreateHTagDismissed() // I GET THE ERROR HERE 'Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value'
        })
    }
}

您需要在這里設置代表

func popUpDimissed() {
   let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "secondPopUp") as! SecondPopUp
   secondPopUpVC.createHTagDelegate = self
   present(secondPopUpVC, animated: true, completion: nil)
}

順便說一句在viewDidLoad里面的這段代碼

let secondPopUpVC = storyboard?.instantiateViewController(withIdentifier: "createTag") as! SecondPopUp
secondPopUpVC.createHTagDelegate = self

沒有用

編輯:

你必須在里面做同樣的事情

func popUpCreateHTagDismissed() {
    let firstPopUpVC = storyboard?.instantiateViewController(withIdentifier: "firstPopUp") as! FirstPopUp
    firstPopUpVC.popUpDismissedDelegate = self 
    present(firstPopUpVC, animated: true, completion: nil)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM