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

转载请注明出处,原文网址: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;
@end
MainViewController.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

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


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

点击下载本文源代码

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值