IOS programming multi-view application experience after completion

Source: Internet
Author: User

IOS programming multi-view application experience after completion

The development platform using xcode is different from the development platform using C ++ builder:

1. xcode creates a window-base project similar to a Forms Application project created in CB. A Class class is created as the main form and three files are automatically generated;

2. The new form created by xcode is not linked to the class in the code. You must modify the class of file's owner in the identity Checker to nsobject by default, change it to the class defined in the Code, for example, blueviewcontroller.

For example:

Double-click blueview. XIB, click the file's owner icon, and press IPv4 to open the identity checker. The default class of the file's owner is nsobject and changed to blueviewcontroller.

Figure 6-14 modify the identity checker

3. Writing code in xcode is more convenient than writing code in CB, because the relevant code is automatically searched and displayed for the operator to choose, therefore, a small application has a lot of code and is very long.

I learned how to write a multi-view application:

Steps:

1. Create a project

2. Create a view controller and a NIB file

(1) Click the classes folder in the groups & files pane, and then press sort N or select new file… from the File menu... Select cocoa touch classes from the left-side pane. You can see templates for common cocoa touch classes. Select uiviewcontroller subclass. In the right pane, you can see a check box marked with XIB for user interface. If this check box is selected, click it to cancel the selection.

Click Next and enter the name switchviewcontroller. m to ensure that also create "switchviewcontroller. h" is selected, and then click Finish.

(2) create two nib files, which correspond to the two content views just created. Click the resource folder in the Group & files pane to create them in the correct location. Press limit N or select new file... from the File menu ..., In the left-side pane, select user interface under the iPhone OS title and select the view XIB template icon. This will create a NIB file with a content view, click Next, and enter blueview. XIB.

1. Modify application delegate

(1) Click the view_switchappdelegate.h file in the groups & files pane and modify the file:

# Import <uikit/uikit. h>

@ Class switchviewcontroller;

@ Interface view_switcherappdelegate: nsobject <uiapplicationdelegate> {

Iboutlet uiwindow * window;

Iboutlet switchviewcontroller * switchviewcontroller;

}

@ Property (nonatomic, retain) uiwindow * window;

@ Property (nonatomic, retain) switchviewcontroller * switchviewcontroller;

@ End

(2) Modify view_switherappdelegate.m and add the code to add the view of the root controller to the main window of the application:

# Import "view_switherappdelegate.h"

# Import "switchviewcontroller. H"

@ Synthesize window;

@ Synthesize switchviewcontroller;

-(Void) applicationdidfinishlaunching :( uiapplication *) Application {

// Override point for customization after app launch

[Window addsubview: switchviewcontroller. View];

[Window makekeyandvisible];

}

-(Void) dealloc {

[Window release];

[Switchviewcontroller release];

[Super dealloc];

}

2. Modify switchviewcontroller. h

# Import <uikit/uikit. h>

@ Class blueviewcontroller;

@ Class yellowviewcontroller;

@ Interface switchviewcontroller: uiviewcontroller {

Yellowviewcontroller * yellowviewcontroller;

Blueviewcontroller * blueviewcontroller;

}

@ Property (retain, nonatomic) yellowviewcontroller * yellowviewcontroller;

@ Property (retain, nonatomic) blueviewcontroller * blueviewcontroller;

-(Ibaction) switchviews :( ID) sender;

@ End

3. Modify the mainwindow. XIB File

(1) double-click mainwindow. XIB and open it in interface builder. Add a view controller from the library)

Figure 6-10 Add a View Controller

(2) Click the View Controller icon in the nib main window, and press consumer 4 to open the identity Checker: change its class to switchviewcontroller.

Figure 6-11 identity checker

(3) drag a view from the library to the window corresponding to switchviewcontroller.

Figure 6-12 add a view

(4) drag a toolbar from the library to the view and place it at the bottom. Double-click the button in the toolbar to change its title to switch view. Press ctrl to drag the button to the switch View Controller icon and select switchviews: operation.

Figure 6-13 Add a toolbar

(5) press Ctrl and drag the view_swicherappdelegate icon to the switchviewcontroller icon, and then select the swicthviewcontroller output port.

4. Modify switchviewcontroller. m

# Import "switchviewcontroller. H"

# Import "blueviewcontroller. H"

# Import "yellowviewcontroller. H"

@ Implementation switchviewcontroller

@ Synthesize blueviewcontroller;

@ Synthesize yellowviewcontroller;

-(Void) viewdidload

{

Blueviewcontroller * bluecontroller =

[[Blueviewcontroller alloc] initwithnibname: @ "blueview" Bundle: Nil];

Self. blueviewcontroller = bluecontroller;

[Self. View insertsubview: bluecontroller. View atindex: 0];

[Bluecontroller release];

}

-(Ibaction) switchviews :( ID) sender

{

If (self. yellowviewcontroller. View. superview = nil)

{

If (self. yellowviewcontroller = nil)

{

Yellowviewcontroller * yellowcontroller =

[[Yellowviewcontroller alloc]

Initwithnibname: @ "yellowview" Bundle: Nil];

Self. yellowviewcontroller = yellowcontroller;

[Yellowcontroller release];

}

[Blueviewcontroller. View removefromsuperview];

[Self. View insertsubview: yellowviewcontroller. View atindex: 0];

}

Else if (self. blueviewcontroller = nil)

{

Blueviewcontroller * bluecontroller =

[[Blueviewcontroller alloc] initwithnibname: @ "blueview" Bundle: Nil];

Self. blueviewcontroller = bluecontroller;

[Bluecontroller release];

}

[Yellowviewcontroller. View removefromsuperview];

[Self. View insertsubview: blueviewcontroller. View atindex: 0];

}

-(Void) didreceivememorywarning

{

[Super didreceivememorywarning];

If (self. blueviewcontroller. View. superview = nil)

Self. blueviewcontroller = nil;

Else

Self. yellowviewcontroller = nil;

}

-(Void) dealloc {

[Yellowviewcontroller release];

[Blueviewcontroller release];

[Super dealloc];

}

5. Implement the content View

(1) modify the Declaration file of the blueview content View Controller:

# Import <uikit/uikit. h>

@ Interface blueviewcontroller: uiviewcontroller {

}

-(Ibaction) bluebuttonpressed :( ID) sender;

@ End

(2) modify the Declaration file of the yellowview controller:

# Import <uikit/uikit. h>

@ Interface yellowviewcontroller: uiviewcontroller {

}

-(Ibaction) yellowbuttonpressed :( ID) sender;

@ End

(3) double-click blueview. XIB, click the file's owner icon, and press Terminal 4 to open the identity checker. The default class of the file's owner is nsobject and changed to blueviewcontroller.

Figure 6-14 modify the identity checker

(4) Click the view icon, and then consumer 1 calls up the property checker. Click the color marked with background to set the background color of this view to blue.

Figure 6-15 modify the view background color

(5) drag a round rect button from the database to the window. Double-click the button and change its title to press me. Put the button in a proper place. Switch to the connection checker, drag the touch up inside event to the file's owner icon, and select bluebuttonpressed: operation method.

Figure 6-16 connection operations

(6) connect the view output port of blueviewcontroller to the view in nib.

Figure 6-17 connection view output port

(7) double-click the yellowview. XIB file to make the same changes to the NIB file, change the button title to press me, too, and set the background color to yellow.

(8) how to open the blueviewcontroller. M file

-(Ibaction) bluebuttonpressed :( ID) sender

{

Uialertview * Alert = [[uialertview alloc]

Initwithtitle: @ "Blue View button pressed"

Message: @ "you pressed the button on the Blue View"

Delegate: Nil

Cancelbuttontitle: @ "Yep, I did ."

Otherbuttontitles: Nil];

[Alert show];

[Alert release];

}

(9) how to open the yellowviewcontroller. M file

-(Ibaction) yellowbuttonpressed :( ID) sender

{

Uialertview * Alert = [[uialertview alloc]

Initwithtitle: @ "yellow View button pressed"

Message: @ "you pressed the button on the yellow view"

Delegate: Nil

Cancelbuttontitle: @ "Yep, I did ."

Otherbuttontitles: Nil];

[Alert show];

[Alert release];

}

6. Create a conversion action

(1) Modify switchviews: operation method.

-(Ibaction) switchviews :( ID) sender

{

[Uiview beginanimations: @ "view flip" context: Nil];

[Uiview setanimationduration: 1.25];

[Uiview setanimationcurve: uiviewanimationcurveeaseinout];

If (self. yellowviewcontroller. View. superview = nil)

{

If (self. yellowviewcontroller = nil)

{

Yellowviewcontroller * yellowcontroller =

[[Yellowviewcontroller alloc] initwithnibname: @ "yellowview"

Bundle: Nil];

Self. yellowviewcontroller = yellowcontroller;

[Yellowcontroller release];

}

[Uiview setanimationtransition: uiviewanimationtransitionflipfromright

Forview: Self. View cache: Yes];

[Blueviewcontroller viewwillappear: Yes];

[Yellowviewcontroller viewwilldisappear: Yes];

[Blueviewcontroller. View removefromsuperview];

[Self. View insertsubview: yellowviewcontroller. View atindex: 0];

[Yellowviewcontroller viewdiddisappear: Yes];

[Blueviewcontroller viewdidappear: Yes];

}

Else

{

If (self. blueviewcontroller = nil)

{

Blueviewcontroller * bluecontroller =

[[Blueviewcontroller alloc] initwithnibname: @ "blueview"

Bundle: Nil];

Self. blueviewcontroller = bluecontroller;

[Bluecontroller release];

}

[Uiview setanimationtransition: uiviewanimationtransitionflipfromleft

Forview: Self. View cache: Yes];

[Yellowviewcontroller viewwillappear: Yes];

[Blueviewcontroller viewwilldisappear: Yes];

[Yellowviewcontroller. View removefromsuperview];

[Self. View insertsubview: blueviewcontroller. View atindex: 0];

[Blueviewcontroller viewdiddisappear: Yes];

[Yellowviewcontroller viewdidappear: Yes];

}

[Uiview commitanimations];

}

7. Run the program and click press me.

Figure 6-18 Running Effect

8. Click switch view, and then press me and too.

Related Article

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.