我在第一个视图控制器中有函数调用(弹出视图),在应用程序中只需调用一次。从那时起,每当我返回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)
}
我在堆栈溢出中尝试了以下代码和之前的其他资源,但是没有用。.
///// 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()
}
}
答案 0 :(得分:3)
也许可行。在您的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),则此方法将无效。
答案 1 :(得分:1)
您应该在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
}
希望有帮助!
答案 2 :(得分:0)
根据视图控制器的生命周期,viewDidLoad
方法仅被调用一次。所以您应该只在那儿调用您的方法。
或者您可以尝试以下代码:
var isScreenAppeared = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !isScreenAppeared {
popView()
}
isScreenAppeared = true
}
答案 3 :(得分:0)
在下面的代码中,尝试一下。
onUpdateNavBarColor
答案 4 :(得分:0)
对我来说,我更喜欢使用延迟加载。这允许不编写任何逻辑,只需要使用Swift lazy var声明即可。像这样:
private lazy var viewDidAppearOnce: Bool = {
popView()
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
_ = viewDidAppearOnce
}
答案 5 :(得分:-1)
如果您只想在您的App中调用该PopView函数一次,请尝试此操作,
在App委托中,设置bool值
int
然后,在第一个视图控制器中尝试一下,
public function submitd()
{
$config['upload_path'] = './images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload',$config);
if($this->upload->do_upload('myfile'))
{
$filedata = $this->upload->data();
$this->resize_image($filedata['full_path']);
}
}
//you can create this function in helper or in same controller.
function resize_image($file_path) {
$CI =& get_instance();
// Set your config up
$config['image_library'] = "gd2";
$config['source_image'] = $file_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['new_image'] = $file_path;
$config['width'] = "320";
$config['height'] = "240";
$config['thumb_marker']=FALSE;
$CI->load->library('image_lib');
$CI->image_lib->initialize($config);
// Do your manipulation
if(!$CI->image_lib->resize())
{
$CI->image_lib->display_errors();
}
$CI->image_lib->clear();
}
然后在viewdidload中这样调用,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.set(true, forKey: "showPop") // like so
return true
}
PopView启动后在您的应用中仅出现一次,并且永远不会再次显示。