简体   繁体   English

在UIViewController,iOS Swift4.2,Xcode10.1中仅调用一次函数

[英]Call Function only once in UIViewController, iOS Swift4.2, Xcode10.1

I have function call (pop view) in my 1st view controller which have to be called only once in app. 我的第一个视图控制器中有函数调用(弹出视图),在应用程序中仅需调用一次。 Since then whenever I return back to 1st View controller the function need not to be called again. 从那时起,每当我返回1st View控制器时,都无需再次调用该函数。

func popView() {
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popView") as! popView
        self.addChild(popOverVC)
        popOverVC.view.frame = self.view.frame
        self.view.addSubview(popOverVC.view)
        popOverVC.didMove(toParent: self)
    }

I have tried the following code and previous other sources in stack overflow, didn't work though.. 我已经尝试了以下代码和堆栈溢出中的其他其他来源,但是没有起作用。

 ///// Once Action in View Controller
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if self.isBeingPresented || self.isMovingToParent {
         // Perform an action that will only be done once
             popView()
        }
    }

Maybe this works. 也许这可行。 In your ViewController, add a static property: 在您的ViewController中,添加一个静态属性:

    static var shouldPop = true

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if isBeingPresented || isMovingToParent {
         // Perform an action that will only be done once
             if (type(of: self).shouldPop) {
                 type(of: self).shouldPop = false
                 popView()
             }
        }
    }

Of course, depending on your setup, this won't work if you have more than one instance of this viewcontroller that should keep their own state on whether popView should be called or not. 当然,根据您的设置,如果您有多个此viewcontroller实例应保持自己的状态(是否应调用popView),则此方法将无效。

You should call this in viewDidLoad method. 您应该在viewDidLoad方法中调用它。 It's called once per UIViewController life cycle. 每个UIViewController生命周期调用一次。 Documentation here . 文档在这里

Just like this: 像这样:

override func viewDidLoad() {
    super.viewDidLoad()

    if self.isBeingPresented || self.isMovingToParent {
     // Perform an action that will only be done once
         popView()
    }
}

If your way is pop view after you was once in view controller you could do like this: 如果在进入视图控制器后使用弹出视图,则可以这样:

    /// bool that help indicate your visit 
    var isViewControllerVisited = false 

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if isViewControllerVisited {
         // Perform an action that will only be done once
             popView()
        }

        //change it here
        isViewControllerVisited = true 
    }

Hope it's help! 希望对您有所帮助!

As per the view controller's lifecycle, the viewDidLoad method only gets called once. 根据视图控制器的生命周期, viewDidLoad方法仅被调用一次。 so you should call your method only there. 所以您应该只在那儿调用您的方法。

Or you can try the following code: 或者,您可以尝试以下代码:

var isScreenAppeared = false

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if !isScreenAppeared {
         popView()
    }
    isScreenAppeared = true
}

Below code, try it.. 在下面的代码中,尝试一下。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    UserDefaults.standard.set(false, forKey: "isPopOverVCPopped")

    if UserDefaults.standard.bool(forKey: "isPopOverVCPopped") == false {
        UserDefaults.standard.set(true, forKey: "isPopOverVCPopped")

        popView()
    }
}

For me I prefer to use lazy loading. 对我来说,我更喜欢使用延迟加载。 This allow not to write any logic, just need to use Swift lazy var declaration. 这允许不编写任何逻辑,只需要使用Swift lazy var声明即可。 Something like this: 像这样:

private lazy var viewDidAppearOnce: Bool = {
    popView()
}()

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    _ = viewDidAppearOnce
}

If you want to call that PopView function only once in you App then try this, 如果您只想在您的应用中调用该PopView函数一次,请尝试此操作,

In App delegate, set bool value 在应用程序委托中,设置布尔值

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     UserDefaults.standard.set(true, forKey: "showPop") // like so
     return true
}

Then, in first view controller try this, 然后,在第一个视图控制器中尝试此操作,

  func hasLaunchPop() {
   let isshowPop: Bool = UserDefaults.standard.bool(forKey: "showPop")
        if isshowPop == true  {
            popView()
            UserDefaults.standard.set(false, forKey: "showPop")
        }
    }

then in viewdidload call like this, 然后在这样的viewdidload调用中,

override func viewDidLoad() {
    super.viewDidLoad()
    hasLaunchPop()
}

So that PopView appears only once in your app when its launched and will never show up again. 因此,PopView启动时在您的应用程序中仅出现一次,并且永远不会再次显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM