We often need to implement many functions during application development. In this case, we often need to build a multi-view application. On the iOS platform, we can use uinavigationcontroller to implement this.
Create a view-based project and modify the code in appdelegate. h as follows:
#import <UIKit/UIKit.h>@class ViewController;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) ViewController *viewController;@property (nonatomic, retain) UINavigationController *navController;@end
We have defined a navigation controller. Next we will add a navigation controller and a view. Modify the code in appdelegate. M:
@synthesize navController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.navController = [[UINavigationController alloc] init]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; [self.navController pushViewController:self.viewController animated:YES]; [self.window addSubview:self.navController.view]; [self.window makeKeyAndVisible]; return YES;}
Here we initialize our navigation controller, add a subview for the navigation controller, and display it in the window.
Now we need to define a button event in viewcontroller. h to respond to the button event in our Root View. (Start button)
- (IBAction)startPressed:(id)sender;
Then implement this method in viewcontroller. M:
- (IBAction)startPressed:(id)sender{ FirstViewController *firstViewController = [[FirstViewController alloc] init]; [self.navigationController pushViewController:firstViewController animated:YES]; [firstViewController release];}
We define a new view controller firstviewcontroller and add it to the navigation controller. Firstviewcontroller implements tableview
For more information about how to use the tableview control, see my blog: tableview control instance developed by iPhone.
To make the program more intuitive, we add the following code to the viewdidload () method to add a title for our application:
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. self.title = @"Cloay";}
Add the following code to firstviewcontroller. M:
#pragma mark - Table view delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // Navigation logic may go here. Create and push another view controller. SecondViewController *detailViewController = [[SecondViewController alloc] init]; NSUInteger row = [indexPath row]; detailViewController.title = [self.listData objectAtIndex:row]; [self.navigationController pushViewController:detailViewController animated:YES]; }
Through this method, we have implemented the process from firstlevel to secondlevel. For example, if we select Kobe
If James is selected:
We can see an image and a next button in the figure. You must have thought of the following code:
- (IBAction)nextPressed:(id)sender{ ThirdViewController *thirdViewController = [[ThirdViewController alloc] init]; [self.navigationController pushViewController:thirdViewController animated:YES]; [thirdViewController release];}
We added a response for the button above, and set the image for imageview in viewdidload:
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib.// self.title = @"Second Level"; NSLog(@"%@", self.title); if ([self.title isEqualToString:@"Kobe"]) { UIImage *portrait = [UIImage imageNamed:@"Kobe-Bryant.jpg"]; self.image.image = portrait; [portrait release]; }else if([self.title isEqualToString:@"James"]){ UIImage *portrait = [UIImage imageNamed:@"James.jpg"]; self.image.image = portrait; [portrait release]; }else{ UIImage *portrait = [UIImage imageNamed:@"ladybug.jpg"]; self.image.image = portrait; [portrait release]; } }
Set the image according to the title. Click Next to enter thirdlevel.
Thirdviewcontroller has only one label control, so no code is pasted here.
This is just a simple demo. You can implement your own functions.
Now, let's write so much. If you have any questions, please leave a message so that you can learn and communicate with each other!
Note: Please indicate the source for reprinting!