ios - 仅显示一次 UIView popOver

标签 ios swift popover

我的 Storyboard中有一个 popOver View 。我有一个关闭按钮,用于从 View 中删除 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
    }
}

这只是测试代码。所以只是尝试这个想法。

最佳答案

只要用户没有从设备上删除应用程序,就使用 userdefault 永久保存数据。

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
}

关于ios - 仅显示一次 UIView popOver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581859/

相关文章:

iphone - iOS 中 UI 控件状态管理的设计模式

ios - 将 NSMutableArray 排序为 NSMutableArray

android - 如何在 Flutter 中禁用 FlatButton 的飞溅突出显示?

ios - 如何从编程的 uibutton 传递数据信息到函数?

ios - 将打印重定向到文件,例如将 NSLog 重定向到文件

ios - UIPopoverPresentationController 不调用 UIAdaptivePresentationControllerDelegate 方法

javascript - Bootstrap 弹出窗口未显示在所有元素之上

ios - Firebase和 TableView 快速复制数据

ios - Firebase 数据库在批准后保存数据

ios - 弹出 View 不显示 subview