Recently, I found that the example is a little old and xcode has changed. So I wrote a post about view switching and provided a reference for myself.
Let's get started!
Implementation:
Here we implement switching between two views.
1. We need two views (XIB file-firstview. XIB secondview. XIB)
2. We need three view control classes: firstviewcontroller, secondviewcontroller, and switchviewcontroller.
Why do we need three view control classes?
Firstview -- firstviewcontroller
Secondview -- secondviewcontroller
Switchviewcontroller -- convert -- [firstview <-> sencondview]
Code Implementation:
I.Build a project---
1. Open xcode file --> New... --> project -->
SelectEmpty Application (xcode 3YesWindow-base Application ).
Click Next to name the project changeviewtest.
Do not select the three.
Here,Our project has been initially set up..
----------II.Create view and View class--------
1. Under the project package, right-click --- New File --- and select user interface on the left.
Select View
2. Next step,InDevice FamilySelectIphone
3. Next, name it firstview.
The generated firstview. XIB is the corresponding file.
Next we will use the same method to create secondview. XIB;
4. Add controls in * view. XIB. Here I will add a button and label control for the two views respectively ..
5. Now, we create firstviewcontroller and secondviewcontroller for firstview. XIB and secondview. XIB respectively.
6. Create the firstviewcontroller class to inherit from uiviewcontroller.
The secondviewcontroller class is created and inherited from uiviewcontroller.
7. After creation:
8. Associate the view with the view class.
[FirstView-FirstViewController]
9. Associate the view with the view class.
Select firstview. XIB, select File's owner, and write the custom class as firstviewcontroller In the attribute selector .:
10. Associate the view of the current view with outlets.
Left-click View to draw a line
After that, we joined the view and View class.
11. Add a class to control viewcontroller: switchviewcontroller
In the control classSwitchViewControllerAdd code to implement2Views.
Description:InitViewMethod used for initialization during program loadingView,ShowFirstViewMethod to display the firstView,ShowSecondViewUsed to display secondView
Connect:
---------3..Delegate,ActivateSwitchviewcontrollerControl Functions--------
1. Select appdelegate. H. Add the following code:
2.NextAppdelegate. mImplementation:
+ (Appdelegate *) app {
Return (appdelegate *) [[uiapplication sharedapplication] Delegate];
}
3.SetSwicthviewncontrollerSetWindowOfRootviewcontroller,Operation View.
-----Last,ImplementationFirstviewcontrollerAndSecondviewcontrollerView switching----
1.InFirstviewcontrollerAddIbaction,Implement button Switching,As follows::
-(Ibaction) buttonclick: (ID) sender {
[[Appdelegate app]. switchviewcontroller showsecondview];
}
2.InSecondViewControllerAddIBAction,Implement button Switching,As follows::
(If you want to import appdelegate to the project)
Last:Run the program: