iPhone开发【十二】多视图技术总结之四:Segmented Control

本文介绍如何利用 UISegmentedControl 控件来模拟实现 iOS 应用中的多视图切换功能。通过创建并配置多个视图控制器及 UISegmentedControl,实现了根据控件选择的不同来展示相应的视图内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处,原文网址:http://blog.youkuaiyun.com/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如下:

[cpp]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//  
  2. //  AppDelegate.h  
  3. //  MultiView-SegmentControl  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "MainViewController.h"  
  11. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  12.   
  13. @property (strong, nonatomic) UIWindow *window;  
  14.   
  15. @property (strong, nonatomic) MainViewController *mainViewController;   
  16. @end  
  17. </span>  
AppDelegate.m主要修改didFinishLaunchingWithOptions方法,如下:

[cpp]  view plain copy
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">@synthesize window = _window;  
  2. @synthesize mainViewController;  
  3.   
  4. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  5. {  
  6.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  7.     // Override point for customization after application launch.  
  8.       
  9.     self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];  
  10.     //设置根视图控制器  
  11.     self.window.rootViewController = self.mainViewController;  
  12.       
  13.     self.window.backgroundColor = [UIColor whiteColor];  
  14.     [self.window makeKeyAndVisible];  
  15.     return YES;  
  16. }  
  17. </span>  

7、下面开始编写代码,主要修改MainViewController类,MainViewController.h如下:

[cpp]  view plain copy
  1. //  
  2. //  MainViewController.h  
  3. //  MultiView-SegmentControl  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "FirstViewController.h"  
  11. #import "SecondViewController.h"  
  12.   
  13. @interface MainViewController : UIViewController{  
  14.       
  15. }  
  16.   
  17. @property(strong,nonatomic)FirstViewController *firstViewController;  
  18. @property(strong,nonatomic)SecondViewController *secondViewController;  
  19. @property(strong,nonatomic)IBOutlet UISegmentedControl *segmentControl;  
  20. @property(strong,nonatomic)IBOutlet UIToolbar *toolBar;  
  21. -(IBAction)changeView:(id)sender;  
  22. @end  
MainViewController.m如下:

[cpp]  view plain copy
  1. //  
  2. //  AppDelegate.m  
  3. //  MultiView-SegmentControl  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10.   
  11. @implementation AppDelegate  
  12.   
  13. @synthesize window = _window;  
  14. @synthesize mainViewController;  
  15.   
  16. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  17. {  
  18.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  19.     // Override point for customization after application launch.  
  20.       
  21.     self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];  
  22.     //设置根视图控制器  
  23.     self.window.rootViewController = self.mainViewController;  
  24.       
  25.     self.window.backgroundColor = [UIColor whiteColor];  
  26.     [self.window makeKeyAndVisible];  
  27.     return YES;  
  28. }  
  29.   
  30. - (void)applicationWillResignActive:(UIApplication *)application  
  31. {  
  32.     // 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.  
  33.     // 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.  
  34. }  
  35.   
  36. - (void)applicationDidEnterBackground:(UIApplication *)application  
  37. {  
  38.     // 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.   
  39.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  40. }  
  41.   
  42. - (void)applicationWillEnterForeground:(UIApplication *)application  
  43. {  
  44.     // 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.  
  45. }  
  46.   
  47. - (void)applicationDidBecomeActive:(UIApplication *)application  
  48. {  
  49.     // 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.  
  50. }  
  51.   
  52. - (void)applicationWillTerminate:(UIApplication *)application  
  53. {  
  54.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  55. }  
  56.   
  57. @end  

注意,不要忘记设置输出口和操作与xib文件中控件与事件的连接,如下:


8、编译、运行,效果如下:

点击下载本文源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值