在 UIViewController 中只调用一次函数,iOS Swift4.2,Xcode10.1
Call Function only once in UIViewController, iOS Swift4.2, Xcode10.1
我的第一个视图控制器中有函数调用(弹出视图),它只能在应用程序中调用一次。从那时起,每当我 return 回到第一个视图控制器时,就不需要再次调用该函数。
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)
}
我已经在堆栈溢出中尝试了以下代码和之前的其他来源,但是没有用..
///// 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()
}
}
您应该在 viewDidLoad 方法中调用它。每个 UIViewController 生命周期调用一次。
文档 here.
就像这样:
override func viewDidLoad() {
super.viewDidLoad()
if self.isBeingPresented || self.isMovingToParent {
// Perform an action that will only be done once
popView()
}
}
如果你的方式是在你进入视图控制器后弹出视图,你可以这样做:
/// 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
}
希望对您有所帮助!
根据视图控制器的生命周期,viewDidLoad
方法只被调用一次。所以你应该只在那里调用你的方法。
或者你可以试试下面的代码:
var isScreenAppeared = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !isScreenAppeared {
popView()
}
isScreenAppeared = true
}
下面的代码,试一下..
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()
}
}
如果你只想在你的 App 中调用 PopView 函数一次,那么试试这个,
在App delegate中,设置bool值
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.set(true, forKey: "showPop") // like so
return true
}
然后,在第一个视图控制器中尝试这个,
func hasLaunchPop() {
let isshowPop: Bool = UserDefaults.standard.bool(forKey: "showPop")
if isshowPop == true {
popView()
UserDefaults.standard.set(false, forKey: "showPop")
}
}
然后在 viewdidload 中这样调用,
override func viewDidLoad() {
super.viewDidLoad()
hasLaunchPop()
}
这样 PopView 在您的应用程序启动时只会在您的应用程序中出现一次,并且永远不会再次出现。
对我来说,我更喜欢使用延迟加载。这允许不写任何逻辑,只需要使用 Swift lazy var 声明。像这样:
private lazy var viewDidAppearOnce: Bool = {
popView()
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
_ = viewDidAppearOnce
}
也许这行得通。在你的 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()
}
}
}
当然,根据您的设置,如果您有多个此 viewcontroller 的实例,这将无法工作,这些实例应保持自己的状态以决定是否应调用 popView。
我的第一个视图控制器中有函数调用(弹出视图),它只能在应用程序中调用一次。从那时起,每当我 return 回到第一个视图控制器时,就不需要再次调用该函数。
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)
}
我已经在堆栈溢出中尝试了以下代码和之前的其他来源,但是没有用..
///// 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()
}
}
您应该在 viewDidLoad 方法中调用它。每个 UIViewController 生命周期调用一次。 文档 here.
就像这样:
override func viewDidLoad() {
super.viewDidLoad()
if self.isBeingPresented || self.isMovingToParent {
// Perform an action that will only be done once
popView()
}
}
如果你的方式是在你进入视图控制器后弹出视图,你可以这样做:
/// 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
}
希望对您有所帮助!
根据视图控制器的生命周期,viewDidLoad
方法只被调用一次。所以你应该只在那里调用你的方法。
或者你可以试试下面的代码:
var isScreenAppeared = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !isScreenAppeared {
popView()
}
isScreenAppeared = true
}
下面的代码,试一下..
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()
}
}
如果你只想在你的 App 中调用 PopView 函数一次,那么试试这个,
在App delegate中,设置bool值
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.set(true, forKey: "showPop") // like so
return true
}
然后,在第一个视图控制器中尝试这个,
func hasLaunchPop() {
let isshowPop: Bool = UserDefaults.standard.bool(forKey: "showPop")
if isshowPop == true {
popView()
UserDefaults.standard.set(false, forKey: "showPop")
}
}
然后在 viewdidload 中这样调用,
override func viewDidLoad() {
super.viewDidLoad()
hasLaunchPop()
}
这样 PopView 在您的应用程序启动时只会在您的应用程序中出现一次,并且永远不会再次出现。
对我来说,我更喜欢使用延迟加载。这允许不写任何逻辑,只需要使用 Swift lazy var 声明。像这样:
private lazy var viewDidAppearOnce: Bool = {
popView()
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
_ = viewDidAppearOnce
}
也许这行得通。在你的 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()
}
}
}
当然,根据您的设置,如果您有多个此 viewcontroller 的实例,这将无法工作,这些实例应保持自己的状态以决定是否应调用 popView。