Having decided to try programming for iOS, I reviewed a bunch of resources in search of documentation, descriptions, lessons, but most often I came across either lessons on “Creating the first Hello World application” or some highly specialized articles to solve specific problems. Therefore, having begun to understand something about this system, I decided to write an article for beginners about creating a little more complicated than Hello world application. This application consists of two view panes and one Toolbar with a button, when clicked, these panels will switch places. Used to create Xcode 4.1
So let's start from the beginning. Xcode provides several templates for creating applications, but we will not look for easy ways and create an application with the most meager template to later put together all the details on our own.
Go to the menu File -> New -> New project and select a window-based project

We will give our project a name of your choice, choose the iPhone as the device and clear all the checkboxes if they were installed there.

As a result, we get into this approximately picture

Now we need to add 3 more classes to our project - these will be two types that we will change and the controller class to which the Toolbar will belong, it will change the types. To do this, we click the right-click button in the Project Navigator window (this is on the left, where the list of files of our project is displayed), in the menu that opens, select New file, the already familiar file creation window opens and this time we select UIView Controller Subclass.

Click "Next", remove all the checkboxes so that xib files are not automatically created, we will add them later to the project ourselves.

Choose a name, I called it controllerView

Similarly, we create two more classes, firstView and secondView. As a result, we must add 3 pairs of files with the extensions .h and .m. Now we will add two xib files, in which, just, the appearance of those two types, which we will change places, will be contained. To do this, right-click again in the Project Navigator window, in the opened menu, select New file and create a file of type View.

Select iPhone as the device and call them firstView and secondView.
Now that all the necessary files have been created, let's start putting them together to get a working application.
To begin with, we will change the files myViewAppAppDelegate.h and myViewAppAppDelegate.m in which we add an instance of our controllerView controller to the main window, for this we add the controllerView class to the myViewAppAppDelegate.h file, create an instance of the class and add it to the main view already in myViewAppAppDelegate.m .
MyViewAppAppDelegate.h file#import <UIKit/UIKit.h> @class controllerView; // @interface myViewAppAppDelegate : NSObject <UIApplicationDelegate>{ UIWindow *window; controllerView *ControllerView; // } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet controllerView *ControllerView; @end
')
File myViewAppAppDelegate.m #import "myViewAppAppDelegate.h" #import "controllerView.h" @implementation myViewAppAppDelegate @synthesize window = _window; @synthesize ControllerView; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window addSubview:ControllerView.view];
We will also prepare the file controllerView.h, describe there all the classes and methods we need, in order to later associate them with our objects.
File controllerView.h #import <UIKit/UIKit.h> @class firstView; // @class secondView; // @interface controllerView : UIViewController { firstView *FirstView; secondView *SecondView; } @property (retain, nonatomic) firstView *FirstView; @property (retain, nonatomic) secondView *SecondView; - (IBAction)changeView:(id)sender; // @end
Now you need to add the controller object itself to the file with the main window MainWindow.xib and associate it with our class. To do this, open the MainWindow.xib file, in the library (bottom right) select the View Controller element

and drag it over the main view

After that, in the icon column on the left, a new icon should be added, corresponding to our controller. We need to associate our controller with our controllerView class, to do this, select the controller object and in the Identity Inspector window (the upper right window, see the previous figure) select controllerView from the list of classes. To check, we hover the cursor over the icon of our controller in the icon column and the Controller View signature should appear there.
Now we take the View object in the library and drag it to our controller.

And set the Toolbar, for this in the library, take the Toolbar object in the library and set it at the top of our View object. Double-clicking on the button, change its name to "Change species."

We associate a button object with the method we declared. To do this, select the button, hold down the control and click the left mouse button, drag the line to the Files's owner icon and select the changeView method from the list that opens. Then select the My View App App Delegate icon, hold down the control and left mouse button and drag the line to the Controller View icon and select the controller View from the list that opens. The main part of the lesson is over, now there is some more code, a couple of links and everything is ready.
Modify the controllerView.m file, add the method code for the view swap
File controllerView.m #import "controllerView.h" #import "firstView.h" #import "secondView.h" @implementation controllerView @synthesize FirstView; @synthesize SecondView; - (void) viewDidLoad {
Now it remains to bring to mind our species files. Select the file firstView.xib and add a place for our Toolbar so that it does not overlap it. To do this, select the main view and the Simulated Metrics window opposite the Top Bar, select the Navigation Bar.

Then we will make this view an instance of the class firstView, for this, as we have done in the main window, in the Identity Inspector window selects firstView from the list of classes.

And the final touch. Select the File's Owner icon, hold down the control and left mouse button and drag the line to our view, in the opened menu, select View. Then we repeat the same procedure for the secondView.xib file, only we bind it to the secondview class. We also make changes to the appearance of our first and second types, so that we can distinguish them when they change, add some buttons or pictures.
That's all, the project can be started and if you did everything correctly, then you will have an application with one active button that swaps two views.
The idea of the lesson was peeped in the book “Beginning iPhone 4 Development Exploring the iOS SDK” by David Mark, Jeff LaMarche, Jack Nutting, but the implementation and description is mine.