Write a simple view switch application

Source: Internet
Author: User

In IOS applications, views are often switched. applications that do not switch views are a few and simple.

The implementation process is described as follows. This article references beginning iPhone
Development, but it may be space limitations. The author wrote a big example in one breath. I will implement it in several stages. In this way, IB (Interface
Builder. In addition, some additional content is simplified for better understanding.

The overall effect is as follows:

Click the "Switch" button and switch from the first screen to the second screen.

Create a blank screen with buttons

In this example, the switch button does not work. In this example, neither the first screen nor the second screen is displayed.

Create a Windows based application:

After the project is generated, create switchviewcontroller. This controller is used to switch the subview and switch to the first and second screens. Of course, in this example, only the first screen is displayed.

Under the classes directory, two files of the controller are created:

Write code and introduce switchviewcontroller in delegate. In the delegate header file:

# Import <uikit/uikit. h>
# Import "switchviewcontroller. H"

@ Interface switchsampleappdelegate: nsobject <uiapplicationdelegate> {
Uiwindow * window;
Switchviewcontroller * switchviewcontroller;
}

@ Property (nonatomic, retain) iboutlet uiwindow * window;
@ Property (nonatomic, retain) iboutlet switchviewcontroller * switchviewcontroller;

@ End

In the corresponding M file:

# Import "switchsampleappdelegate. H"

@ Implementation switchsampleappdelegate

@ Synthesize window;
@ Synthesize switchviewcontroller;

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {
// Override point for customization after application launch.
[Window addsubview: switchviewcontroller. View];
[Window makekeyandvisible];
Return yes;
}

-(Void) dealloc {
[Window release];
[Switchviewcontroller release];
[Super dealloc];
}

@ End

Next, set the interface content. Double-click mainwindow. XIB:

Drag View Controller to mainwindow. XIB:

The generated View Controller window is as follows:

Change the corresponding class to switchviewcontroller:

Drag a view and a toolbar to the window:

Change the button text in the toolbar to "Switch ":

Next, to associate the created view (that is, the view just dragged to the window) with switchviewcontroller, select view first:

If the option is incorrect, the controller rather than the included view may be selected. On the Connections tab, create an association:

Select the view and the associated View:

Run on the simulator and you can see:

However, you cannot click this button. Add click processing code. You can add the following in switchviewcontroller. h:

-(Ibaction) switchviews :( ID) sender;

Then add the implementation of this method to the M file:

-(Ibaction) switchviews :( ID) sender {
Nslog (@ ">>> touch toolbar ");
}

The buttons in the view need to be associated with the method implemented above:

Then select the switchviews entry. Start the application again. You can find it in the log:

[Session started at 10:42:26 + 0800.]
10:42:29. 981 nextswitchdemo [70102: 207] >>>> touch Toolbar

Add the first screen

First, create the controller of the first screen, that is, firstviewcontroller:

The process is similar to that of switchviewcontroller.

Then you need to create the XIB file on the first screen, that is, firstview, which should be created under resouces:

Open with IB and change the background color:

Add text:

Then, set the association between firstview and controller.

Modify the file's owner to firstviewcontroller. The default value is nsobject:

Then, set the view Association to the file' owner, that is, firstviewcontroller.

Add the screen view to the parent view as its subview, that is, the view associated with switchviewcontroller. You need to write code in the switchviewcontroller. h file:

# Import <uikit/uikit. h>
# Import "firstviewcontroller. H"

@ Interface switchviewcontroller: uiviewcontroller {
Firstviewcontroller * firstviewcontroller;
}
@ Property (nonatomic, retain) firstviewcontroller * firstviewcontroller;
-(Ibaction) switchviews :( ID) sender;

@ End

Add the following to the M file:

@ Synthesize firstviewcontroller;

...

-(Void) viewdidload {
Firstviewcontroller = [[firstviewcontroller alloc] initwithnibname: @ "firstview" Bundle: Nil];
[Self. View insertsubview: firstviewcontroller. View atindex: 0];
[Super viewdidload];
}

The first screen will appear at the beginning of this article.

Add the second screen and switch through the button

Similar to the above practice, you can create the controller and view file of the second screen.

The main difference is that the switchviewcontroller method requires:

-(Void) viewdidload {
Firstviewcontroller = [[firstviewcontroller alloc] initwithnibname: @ "firstview" Bundle: Nil];
Secondviewcontroller = [[secondviewcontroller alloc] initwithnibname: @ "secondview" Bundle: Nil];
[Self. View insertsubview: firstviewcontroller. View atindex: 0];
[Super viewdidload];
}

-(Ibaction) switchviews :( ID) sender {
If (firstviewcontroller. View. superview = nil ){
[Secondviewcontroller. View removefromsuperview];
[Self. View insertsubview: firstviewcontroller. View atindex: 0];
} Else {
[Firstviewcontroller. View removefromsuperview];
[Self. View insertsubview: secondviewcontroller. View atindex: 0];
}

}

In this way, you can switch the view by button.

Add a transition animation to the switch View

Modified the switchviews method of switchviewcontroller:

-(Ibaction) switchviews :( ID) sender {
[Uiview beginanimations: @ "view flip" context: Nil];
[Uiview setanimationduration: 1.25];
[Uiview setanimationcurve: uiviewanimationcurveeaseinout];
If (firstviewcontroller. View. superview = nil ){
[Uiview setanimationtransition:
Uiviewanimationtransitionflipfromright
Forview: Self. View cache: Yes];
[Secondviewcontroller. View removefromsuperview];
[Self. View insertsubview: firstviewcontroller. View atindex: 0];
[Firstviewcontroller viewwillappear: Yes];
[Secondviewcontroller viewwilldisappear: Yes];
[Secondviewcontroller viewdiddisappear: Yes];
[Firstviewcontroller viewdidappear: Yes];
} Else {
[Firstviewcontroller. View removefromsuperview];
[Self. View insertsubview: secondviewcontroller. View atindex: 0];
}
[Uiview commitanimations];
}

You can achieve the animation effect from the second screen to the first screen, and so on from the first screen to the second screen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.