`

iPhone开发多视图技术总结之五:Segmented Control .

    博客分类:
  • ios
ios 
阅读更多
这是iPhone开发多视图技术系列最后一篇,说说使用SegmentedControl实现视图切换。

实现的功能:通过UISegmentedControl模拟多视图切换。

关键词:多视图UISegmentedControl


UISegmentedControl是一个横向的组件,由多部分组成,每一部分都是一个独立的按钮,一般用来切换视图的显示模式或者在几项之间做单选。

这个控件并不是用来实现多视图切换的,实际开发中也几乎不用它来做多视图切换,此博文仅为模拟多视图应用。



1、创建一个Empty Application工程,命名为:MultiView-Navigation,如下图

[img]

[/img]





2、选中工程中的Group MultiView-Tab,然后按住CMD(Windows键)+N,新建视图控制器MainViewController,如下图
[img]

[/img]





3、依照上步,新建视图控制器FirstViewController、SecondViewController






4、修改MainViewController.xib,添加一个ToolBar控件,一个Segmented Control 控件,两个Fixed Space Bar Button Item控件,如下:

[img]

[/img]







5、修改FirstViewController.xib、SecondViewController.xib,各添加一个Label控件,如下:

[img]

[/img]    [img]

[/img]






6、修改AppDelegae类,AppDelegate.h如下:
#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方法,如下:
- (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如下:

#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]];
    // 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



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

[img]

[/img]







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

[img]

[/img]   [img]

[/img]
  • 大小: 164 KB
  • 大小: 164.1 KB
  • 大小: 36.6 KB
  • 大小: 9.1 KB
  • 大小: 9.3 KB
  • 大小: 28.3 KB
  • 大小: 71.1 KB
  • 大小: 74 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics