How To Swap View In iPhone?

There is one way  of handling autorotation, and it’s an option you’ll likely only use in the case of very complex interface. We just design the landscape and portrait views separately and then swap them out when the phone is rotated.

Step 1: Create a new project in Xcode using the view-based application.

Step 2: Double click .xib file to open the file in interface Builder. First single click View icon, and press delete botton. Next drag over two UIView from library . Single click the view to select it and change name one view Portrait and other Landscape. Now , control drag from File’s Owner to the Portrait icon and select portrait outlet. Then, control drag from File’s Owner a second time to the Landscape icon, and select Landscape outlet. Double click the icon called Landscape, and bring up size inspector. Change the view value 480 pixel wide and 300 pixels tall. Now drag two Round Rect Buttons over from the library to the Landscape view. Double click the left button, and give the title Foo, and then double click the right one , and give it a title of Bar. Control drag from the File’s Owner icon to the Foo button, and assign it to the landscapeFoo outlet, and then do the same thing to assign the Bar button to the landscapeBar outlet. Single click Foo button, and bring to connection inspector. Drag touch up inside to File’s Owner icon. (See figure 1)

Figure 1: Landscape View.

Step 3: We need to put the code in place to handle the swap and the button taps.




Step 4:  The core graphics framework is not included in our Xcode project. We  have to manually link that framework into our project. The iPhone OS frameworks can be found here :

Step 5: Now compile and rum it on the simulator.(See figure 2)

Figure 2: Portrait View.

Leave a Comment:

Erwin says June 29, 2010

how do you control drag from file owner to both views (landscape and portrait)?
If I control drag the from file owner to landscape its fine, but when i control drag the second time but going to portrait view, the connection from landscape is deleted and replaced by the new connection to portrait view.

Swapping views | Allscrappedup says August 21, 2011

[…] How to Swap View in Iphone? | iPhone Tutorial | iPhone iOS4 iPad …Jan 28, 2010 … There is one way of handling autorotation, and it’s an option you’ll likely only use in the case of very complex interface. […]

Add Your Reply