iPhone开发【十二】多视图技术总结之四:Segmented Control
发布日期:2021-09-28 18:46:28 浏览次数:11 分类:技术文章

本文共 4949 字,大约阅读时间需要 16 分钟。

转载请注明出处,原文网址:作者:张燕广

这是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] 
  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] 
  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] 
  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] 
  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、编译、运行,效果如下:

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8560287 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【十三】动画效果之最简单的动画——动态加载图片
下一篇:iPhone开发【十一】多视图技术总结之四:Page Control

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月06日 22时11分36秒