0%

iOS学习——Segmented Control

学会Segmented Control点击切换视图

故事版:

故事版

<br/ ><br/ >
要注意的是,这里有两个view,一个是被隐藏的,我放在了同一个位置
被隐藏的view


功能描述:

1
2
3
4
5
6
7
8
9
10
11
st=>start: 开始
e=>end: 结束
op=>operation: 选择一个segment
s1=>operation: 个人信息segment
s2=>operation: 学校信息segment
r1=>operation: 个人信息页面
r2=>operation: 学校信息页面
cond=>condition: 判断选择哪一个segment
st->op->cond
cond(yes)->s1->r1->e
cond(no)->s2->r2->e

代码:

把第三个View Controller换成自己写的类showLabelViewController(因为开始是做点击按钮显示名字事件的,所以取了这个名字)
<br/ >

  • showLabelViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//
// 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

<br/ >

  • showLabelViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// 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 = @"Lotty周小鱼";
}

- (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
Segmented Control切换视图


有什么问题都可以在博文后面留言,或者微博上私信我。

博主是 iOS 妹子一枚。

希望大家一起进步。

我的微博:Lotty周小鱼