looping using the same view controller

Discussion in 'iOS Programming' started by theprizerevealed, Jan 21, 2017.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    hi,

    I wonder if it's possible to loop through the same view controller in some analogous fashion to segueing?

    Basically I want to use a single view controller to prompt for an input then the user pushes a button on that view controller and the output is displayed on that same view controller then I want the process to begin again with a new prompt for input.

    Is there some analogous code to the the segue function? - or is it as simple as a while loop? thanks
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What you're describing is something like a web browser. The browser displays a certain page. The user clicks a link on that page and a new page is displayed but on the same screen. And of course you can move forward and back through the pages.

    Consider a UITableView. You can change its data and then reloadData(). This is very much like what you want. Design your own data model and implement your own reloadData() method that displays the content described by the data. Naturally you connect your button to the reloadData() method. Segues and loops don't apply.
     
  3. theprizerevealed thread starter macrumors member

    Joined:
    Feb 26, 2016
    #3
    well, I should have mentioned that I wish to use buttons and the table view doesn't appear quite suitable for my wishes. However I have found some helpful code here: https://sketchytech.blogspot.com/2012/11/instantiate-view-controller-using.html .

    However, I am concerned slightly because the memory usage seems to grow the more that you use the program according to Xcode. Here is the code:

    Code:
     let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as UIViewController
                // .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it
               
                self.present(viewController, animated: false, completion: nil) 
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Right. In my example you don't build a new view controller. You use the EXACT SAME INSTANCE OF THE VIEW CONTROLLER. The table view is just an analogy. Instead of the code that you showed you need to write a reloadData() method for your view controller. Then when the user taps the button you update the data model and call reloadData(). You don't build a new view controller.
     
  5. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #5
    What this says is correct.

    Everytime you create a new controller, or any object, you take up a slice of memory. Although Apple's memory management is good and no longer requires a manual de-allocation of an object you still chew up memory if you create a new object. Create the view once and just re-use it as needed. It's the lost art of pointer management.
     

Share This Page