簡體   English   中英

僅顯示一次UIView popOver

[英]Present a UIView popOver only once

我的故事板上有一個popOver視圖。 我有一個關閉按鈕,可以從視圖中刪除popOver。 但是我也想檢查popOver是否已經啟動/顯示給用戶。

我是否被迫在AppDelegate中使用userDefaults? 我可能沒有權限在需要執行此操作的實際生產應用中更改AppDelegate。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var popOne: UIButton!
    @IBOutlet weak var popTwo: UIButton!

    // Using the popOver UIView NOT as a specialized UIView Class
    @IBOutlet var PopOver: UIView!

    @IBOutlet weak var dismissButton: UIButton!
    @IBOutlet weak var popOverLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        PopOver.layer.cornerRadius = 10
    }

    func checkStatus() {
        // check if the popOver view has already been presented
        // ONLY present popOver is user hasn't seen it yet.

        // if userStatusType == .complete
        // run confetti popOver
        // else if != .complete
        // run the non-confetti overlay
    }

    // popOver One Action
    @IBAction func popOneAction(_ sender: Any) {
        // dismiss buttons enabled
        popOne.isEnabled = false
        popOne.backgroundColor = .green
        popTwo.isEnabled = false
        // present the popover
        self.view.addSubview(PopOver)
        // set the confetti to load only in popOver View
        PopOver.clipsToBounds = true
        PopOver.startConfetti()
        // set the popOver to center view
        PopOver.center = view.center
        // modify popOver UI elements
        popOverLabel.textColor = .green
        popOverLabel.text = "GREEN and POP ONE. Notice Buttons in Background are now dismissed. So Press the popOver Button to remove the popOver and return to main VC."
        // create a layer over the background VC
        // Set the Overlay Color to a light gray
        // Set the alpha / opacity to under 50% to keep the main UI still visible.
        self.view.backgroundColor = .gray
        self.view.alpha = 0.3
    }

    // popOver Two Action
    @IBAction func popTwoAction(_ sender: Any) {
        popOne.isEnabled = false
        popTwo.isEnabled = false
        PopOver.center = view.center
        self.view.addSubview(PopOver)
        popOverLabel.textColor = .cyan
        popOverLabel.text = "YOU DID NOT COMPLETE CHALLENGES THIS MONTH. TRY AGAIN FOR NEXT MONTHS CHALLENGES"
    }

    @IBAction func dismissAction(_ sender: Any) {
        popOne.isEnabled = true
        popTwo.isEnabled = true
        popOne.backgroundColor = #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1) 
        popOverLabel.text = ""

        // Dismiss the popOver
        self.PopOver.removeFromSuperview()
        // Rest the main VC UI
        self.view.backgroundColor = .white
        self.view.alpha = 1
    }
}

這只是測試代碼。 因此,請嘗試一下這個想法。

只要用戶尚未從設備中刪除該應用,請使用userdefualt永久保存數據。

var isItTheFirstTime = "false"

然后再根據需要進行檢查。

if UserDefaults.standard.string(forKey: ConstantsKey.token) == "false"
{
//show the popup and then set the var to true
  UserDefaults.standard.set("true", forKey: isItTheFirstTime)


}
else
{
//don't show the popup
}

使用此擴展程序是為了方便:

extension UserDefaults {
    private static let didShowPopOverKey = "didShowPopOverKey"

    var didShowPopOverKey: Bool {
        get { return bool(forKey: UserDefaults.didShowPopOverKey) }
        set { set(newValue, forKey: UserDefaults.didShowPopOverKey) }
    }
}

popOneAction(sender:)

@IBAction func popOneAction(_ sender: Any) {

    //Check if the popover hasn't been shown before, or else return
    guard !UserDefaults.standard.didShowPopOverKey else {
        return
    }

    //If the popover hasn't been shown before:
    //Before exiting the scope set the key in userdefaults to true
    defer {
        UserDefaults.standard.didShowPopOverKey = true
    }

    //Your code
    popOne.isEnabled = false
    popOne.backgroundColor = .green
    popTwo.isEnabled = false
    // present the popover
    self.view.addSubview(PopOver)
    // set the confetti to load only in popOver View
    PopOver.clipsToBounds = true
    PopOver.startConfetti()
    // set the popOver to center view
    PopOver.center = view.center
    // modify popOver UI elements
    popOverLabel.textColor = .green
    popOverLabel.text = "GREEN and POP ONE. Notice Buttons in Background are now dismissed. So Press the popOver Button to remove the popOver and return to main VC."
    // create a layer over the background VC
    // Set the Overlay Color to a light gray
    // Set the alpha / opacity to under 50% to keep the main UI still visible.
    self.view.backgroundColor = .gray
    self.view.alpha = 0.3
}

暫無
暫無

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

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