Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
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
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
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)
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

bjet767

Suspended
Oct 2, 2010
967
320
Then when the user taps the button you update the data model and call reloadData(). You don't build a new view controller.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.