如何让 ViewController 的 viewDidLoad 方法只调用一次? [英] How to make viewDidLoad method of ViewController call only once?

查看:34
本文介绍了如何让 ViewController 的 viewDidLoad 方法只调用一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中创建了一个 Container View.当 UITableviewCell 和其他 UIViewController 上的用户选项卡被加载到容器视图中时,容器视图会加载其他 UIViewController.我尝试通过在其他 ViewController 类上插入日志消息进行测试,发现每次 viewDidLoadViewDidAppear 方法被调用它指出这些类每次在单元格选项卡上都会被实例化.

I created a Container View in my application. The container view loads other UIViewController when user tabs on a UITableviewCell and other UIViewController gets loaded inside the Container View. I tried testing by inserting log messages on other ViewController class and found out each time viewDidLoad and ViewDidAppear method gets called which states those class gets instantiated everytime on cell tab.

但是在学习苹果文档上的容器视图时https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

But while studying about container view on the apple docs https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

我发现 UITabBarController 也实现了 ContainerView 并在数组中管理它的 UIViewController.我尝试检查日志消息,发现 viewDidLoad 方法只被调用一次,当我们第二次访问选项卡时,只有 viewDidAppear 被调用.所以我真的想知道这背后的幕后事情是什么.他们是否在使用 UnWind segue(我猜不是).

I found UITabBarController also implements ContainerView and manages its UIViewController in array. I tried inspecting the log messages and found that viewDidLoad method gets called only one time and when we visit second time on the tab only viewDidAppear gets called. So i was really wondering what is the behind the scene things in this. Are they using UnWind segue(i guess not).

但是,我按照以下教程使用以下方式创建了容器视图控制器:https://github.com/codepath/ios_guides/wiki/Container-View-Controllers-Quickstart

However i created container View Controller with follwing way following these tutorial: https://github.com/codepath/ios_guides/wiki/Container-View-Controllers-Quickstart as

class ViewController: UIViewController {


    @IBOutlet weak var contentView: UIView!

    var activeViewController : UIViewController? {
        didSet{
            removeInactiveViewController(oldValue)
            updateActiveViewController()
        }
    }


    var cellTxtArrs = ["FIRST","SECOND","THIRD"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var tblView : UITableView = UITableView()
        tblView.frame = CGRectMake(0, 20, 300 ,100)
        tblView.separatorColor = UIColor.clearColor()
        tblView.scrollEnabled = false
        tblView.rowHeight = (30)
        self.view.addSubview(tblView)
        tblView.delegate = self
        tblView.dataSource = self
        tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")


    }



    func removeInactiveViewController(inactiveViewController:UIViewController?){


        if let inActiveVC = inactiveViewController{

            inActiveVC.willMoveToParentViewController(nil)

            inActiveVC.view.removeFromSuperview()

            inActiveVC.removeFromParentViewController()

        }


    }


    func updateActiveViewController(){


        if let activeVC = activeViewController{

            //not necessary
            addChildViewController(activeVC)

            activeVC.view.frame = contentView.bounds

            contentView.addSubview(activeVC.view)

            activeVC.didMoveToParentViewController(self)




        }


    }





    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


extension ViewController : UITableViewDataSource , UITableViewDelegate{

    //MARK:  table view data source methods
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //  cell.textLabel?.font = UIFont(name: label.font.fontName, size: 22)
        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! UITableViewCell
        cell.backgroundColor = UIColor(red: 0.000, green: 0.400, blue: 0.404, alpha: 1.00)
        cell.selectionStyle =  UITableViewCellSelectionStyle.None
        cell.textLabel?.text = self.cellTxtArrs[indexPath.row]
        cell.textLabel?.textAlignment = NSTextAlignment.Left
        cell.textLabel?.textColor = UIColor.whiteColor()

        return cell

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.cellTxtArrs.count

    }


    //MARK: - table view delegate methods

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var row = indexPath.row

        switch row {

        case 0:




                let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let firstVC = storyboard.instantiateViewControllerWithIdentifier("firstVC") as! FirstViewController
                activeViewController = firstVC



        case 1:

                var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let secondVC = storyboard.instantiateViewControllerWithIdentifier("secondVC") as! SecondViewController
                activeViewController = secondVC


        case 2:


                let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let thirdVC = storyboard.instantiateViewControllerWithIdentifier("thirdVC") as! ThirdViewController
                activeViewController = thirdVC



        default:
            println("no index")


        }




    }


}

推荐答案

标签栏控制器有一个子控制器数组,在它们之间切换时保持不变.

The tab bar controller has an array of child controllers which persist when switching between them.

基本上你需要做的是:

  • 创建一个容器来保存子控制器(一个数组)
  • 如果控制器不存在,则将其添加到数组中
  • 重用数组中的控制器(如果存在)

我在很多地方都使用了这个程序.

I use this procedure in quite a few places.

请注意,这对于在内容视图中有导航控制器也很有效,但导航堆栈也会持续存在,这可能不是预期的结果.例如,如果您有 3 个选项卡,并且第一个选项卡导航到第二个视图控制器,即使切换到第三个选项卡并返回到第一个选项卡,它也会留在第二个视图控制器上.

Note this also works great with having navigation controllers in the content view but the navigation stack will persist as well which may not be a desired result. For instance if you have 3 tabs and the 1st tab is navigated to 2nd view controller it will stay on 2nd view controller even after switching to 3rd tab and back to the 1st one.

这篇关于如何让 ViewController 的 viewDidLoad 方法只调用一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆