iPhone Development Part 2
[Swapping between landscape and portrait views]

In this tutorial we will be covering:
Creating an app that allows for iPhone rotation from side to side, as well as creating functions that take multiple arguments and writing a custom class, additionally we will be adding segments to our segmented control and making the various segments act differently based on which one is clicked.

Setting up for the rotation:
So, we have finished the first tutorial, but what now? Well, the one common thought would be to add landscape view mode into the application.

If you are wondering why someone would want to put their application into landscape view here are a few reasons:
- Wider screen
- Increased zoom without having to scroll horizontally
- Easier on the eyes (because most of the information on the screen will be larger than it was in portrait mode)

To start off working on this modification (yes, we are going to be modifying the previous program) we will need to butcher it a bit. This is because we did all the previous code within the AppDelegate class, which is really only meant to delegate the initial memory needed for the application then hand control over to another class. It also cleans up after the application has finished (by releasing the class that had control).

That takes care of defining all the variables we will be using in this class.

NOTICE – One of the variables is a UIScrollView. This allows us to add subViews to it and it will scroll them when the output is outside the bounds of the view, we don’t have to worry about making the scrollbar work it just does.

Now let’s implement that class (myAppView):

@implementation myAppView

The next function is used to load the view of the UIViewController, which is a required function, and is also the standard one for outputting things to the ViewController. Given that we will be setting up all the variables in here:

This goes through creates the scroll view, sets its size to 320 wide and 460 tall with a 20 pixel offset from the top (to not overlap the info bar). Then it sets its content size to 320 by 480 (the max screen size available), and tells it to allow scrolling while still clipping the view at its own bounds.

After that we initialize the label again with the exact same way we did before, but we add it to myView instead of window.

Once that is completed we set up the buttons. While it starts off the same we modify it to have a different style (UISegmentedControlStyleBar) which makes it smaller, and we add a second segment (which will make an alert fire when clicked). The animated:TRUE makes the bar of buttons appear in a wiping tween. This is followed by setting the size for the second segment and the rest is pretty much the same (with the exception of ButtonPress:, which has been changed (added the : ) to allow for send the segment ID to the function.

Now that we have completed this lets set up the button press function:

This tells the UIViewController to auto rotate to all available screen orientations (top, left, right and upside-down). If the class receives a memory warning it sends it up the chain. If it is the top of the chain it will just release all available (it pretty much calls the dealloc function).

So, now we have the shell of our old application. It is now time to insert all the updated code into it so we can see everything working out nicely as the application runs. To do that we actually have very little code to add, in fact all we really need to do is four lines of code to the shell, which changes this code: