转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8213431作者:张燕广
这是iPhone开发多视图技术系列最后一篇,说说使用SegmentedControl实现视图切换。
实现的功能:通过UISegmentedControl模拟多视图切换。
关键词:多视图UISegmentedControl
UISegmentedControl是一个横向的组件,由多部分组成,每一部分都是一个独立的按钮,一般用来切换视图的显示模式或者在几项之间做单选。
这个控件并不是用来实现多视图切换的,实际开发中也几乎不用它来做多视图切换,此博文仅为模拟多视图应用。
1、创建一个Empty Application工程,命名为:MultiView-Navigation,如下图
2、选中工程中的Group MultiView-Tab,然后按住CMD(Windows键)+N,新建视图控制器MainViewController,如下图
3、依照上步,新建视图控制器FirstViewController、SecondViewController
4、修改MainViewController.xib,添加一个ToolBar控件,一个Segmented Control 控件,两个Fixed Space Bar Button Item控件,如下:
5、修改FirstViewController.xib、SecondViewController.xib,各添加一个Label控件,如下:
6、修改AppDelegae类,AppDelegate.h如下:
//
// AppDelegate.h
// MultiView-SegmentControl
//
// Created by Zhang Yanguang on 12-11-21.
// Copyright (c) 2012年 MyCompanyName. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MainViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MainViewController *mainViewController;
@end
AppDelegate.m主要修改didFinishLaunchingWithOptions方法,如下:
@synthesize window = _window;
@synthesize mainViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
//设置根视图控制器
self.window.rootViewController = self.mainViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
7、下面开始编写代码,主要修改MainViewController类,MainViewController.h如下:
// // MainViewController.h // MultiView-SegmentControl // // Created by Zhang Yanguang on 12-11-21. // Copyright (c) 2012年 MyCompanyName. All rights reserved. // #import <UIKit/UIKit.h> #import "FirstViewController.h" #import "SecondViewController.h" @interface MainViewController : UIViewController{ } @property(strong,nonatomic)FirstViewController *firstViewController; @property(strong,nonatomic)SecondViewController *secondViewController; @property(strong,nonatomic)IBOutlet UISegmentedControl *segmentControl; @property(strong,nonatomic)IBOutlet UIToolbar *toolBar; -(IBAction)changeView:(id)sender; @endMainViewController.m如下:
// // AppDelegate.m // MultiView-SegmentControl // // Created by Zhang Yanguang on 12-11-21. // Copyright (c) 2012年 MyCompanyName. All rights reserved. // #import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; @synthesize mainViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil]; //设置根视图控制器 self.window.rootViewController = self.mainViewController; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
8、编译、运行,效果如下:
点击下载本文源代码