iOS学习——Segmented Control
学会Segmented Control点击切换视图
故事版:
要注意的是,这里有两个view,一个是被隐藏的,我放在了同一个位置
功能描述:
Created with Raphaël 2.1.0开始选择一个segment判断选择哪一个segment个人信息segment个人信息页面结束学校信息segment学校信息页面yesno
代码:
把第三个View Controller换成自己写的类showLabelViewController(因为开始是做点击按钮显示名字事件的,所以取了这个名字)
- showLabelViewController.h
//
// showLabelViewController.h
// coderfish004
//
// Created by apple40 on 15-7-20.
// Copyright (c) 2015年 coderfish. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface showLabelViewController : UIViewController
// 按钮点击事件
- (IBAction)actionShowName:(id)sender;
// segment选择事件
- (IBAction)actionSelectPage:(id)sender;
// 个人信息页面
@property (strong, nonatomic) IBOutlet UIView *viewUserPage;
// 学校信息页面
@property (strong, nonatomic) IBOutlet UIView *viewSchoolPage;
// segmented control对象
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;
// 标签对象
@property (strong, nonatomic) IBOutlet UILabel *lblShow;
// 存储变量name
@property (strong, nonatomic) NSString *name;
@end
- showLabelViewController.m
//
// showLabelViewController.m
// coderfish004
//
// Created by apple40 on 15-7-20.
// Copyright (c) 2015年 coderfish. All rights reserved.
//
#import "showLabelViewController.h"
@interface showLabelViewController ()
@end
@implementation showLabelViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.name = @"周凌宇";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionShowName:(id)sender {
self.lblShow.text = self.name;
}
- (IBAction)actionSelectPage:(id)sender {
switch (self.segmentControl.selectedSegmentIndex) {
case 0:
self.viewSchoolPage.hidden = true;
self.viewUserPage.hidden = false;
break;
case 1:
self.viewSchoolPage.hidden = false;
self.viewUserPage.hidden = true;
break;
default:
break;
}
}
@end
结果
最后结果就是点击segment切换不同的View
版权声明:本文为博主原创文章,未经博主允许不得转载。