Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Switching Views in Cocoa Touch

How would one change the view on the screen programmatically in an iPhone app?

I've been able to create navigation view's and programmatically push/pop them to produce this behaviour, but if I wanted to simply change the current view (not using a UINavigation controller object), what is the neatest way to achieve this?

A simple example, imagine an application with a single button, when pressed will display a new view, or possibly one of multiple views depending on some internal state variable.

I have yet to see any examples that attempt to do this, and I don't seem to understand enough about the relationships and initialisation procedure between UIViewController/UIView objects to achieve this programmatically.

like image 965
Akusete Avatar asked Dec 15 '08 23:12

Akusete


2 Answers

I use presentModalViewController:animated: to bring up a settings view from my main window's UIViewController and then when the user presses "done" in the settings view I call dismissModalViewControllerAnimated: from the settings view (reaching back to the parent view) like this:

[[self parentViewController] dismissModalViewControllerAnimated:YES];
like image 56
davidavr Avatar answered Nov 10 '22 03:11

davidavr


You'll want to explore -[UIView addSubview:] and -[UIView removeFromSuperview]. Your base window is a UIView (descendant), so you can add and remove views to it.

like image 32
Ben Gottlieb Avatar answered Nov 10 '22 05:11

Ben Gottlieb