转载请注明出处,原文网址: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如下:
- <span style="font-family:Microsoft YaHei;font-size:18px;">
-
-
-
-
-
-
-
- #import <UIKit/UIKit.h>
- #import "MainViewController.h"
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
-
- @property (strong, nonatomic) UIWindow *window;
-
- @property (strong, nonatomic) MainViewController *mainViewController;
- @end
- </span>
AppDelegate.m主要修改didFinishLaunchingWithOptions方法,如下:
- <span style="font-family:Microsoft YaHei;font-size:18px;">@synthesize window = _window;
- @synthesize mainViewController;
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
-
-
- self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
-
- self.window.rootViewController = self.mainViewController;
-
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
- </span>
7、下面开始编写代码,主要修改MainViewController类,MainViewController.h如下:
-
-
-
-
-
-
-
-
- #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;
- @end
MainViewController.m如下:
-
-
-
-
-
-
-
-
- #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]];
-
-
- 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
- {
-
-
- }
-
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
-
-
- }
-
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
-
- }
-
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
-
- }
-
- - (void)applicationWillTerminate:(UIApplication *)application
- {
-
- }
-
- @end
注意,不要忘记设置输出口和操作与xib文件中控件与事件的连接,如下:
8、编译、运行,效果如下:
点击下载本文源代码