Xcode - storyboard_OC版 07:选择器场景

选择器场景

1 - 现在实现一功能:在 WarriorAdded Scene 中单击指定的 cell 就会打开一个新场景。那么拖进一个新控件 TableViewController,选中 WarriorAdded Scene 的单元格后按住 ctrl 键进行联线:segue 选择 Push 并把它的 Identifier 更名 DetailsStatus;把新场景标题更名成 WarriorStatus ,并把其单元格的 Style 置为 Subtitle 以及把 Identifier 置为 StatusCell

2 - 新建 UITableViewController 子类 WarriorStatusVC,并在 storyboard 中完成与 WarriorStatus 场景关联

3 - 在 WarriorStatusController.m 中配置数据

 1 #import "WarriorStatusVC.h"
 2 #import "Warrior.h"
 3 @interface WarriorStatusVC ()
 4 @end
 5 
 6 @implementation WarriorStatusVC{
 7     
 8     NSArray *_statusArray;
 9 }
10 
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14 
15     _statusArray = [NSArray arrayWithObjects:
16                    [NSString stringWithFormat:@"%@_%d",@"フリーザ",FC_Base],
17                    [NSString stringWithFormat:@"%@_%d",@"ベビー",FC_Base],
18                    [NSString stringWithFormat:@"%@_%d",@"セル",FC_Base],
19                    [NSString stringWithFormat:@"%@_%d",@"ブウ",FC_Base],
20                    [NSString stringWithFormat:@"%@_%d",@"ラズリ",FC_Base],
21                    [NSString stringWithFormat:@"%@_%d",@"ブロリー",FC_Base],nil];
22     
23 }
24 #pragma mark - Table view data source
25 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
26 
27     return 1;
28 }
29 
30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
31     
32      return [_statusArray count];
33 }
34 
35 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
36 
37     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StatusCell" forIndexPath:indexPath];
38     
39         NSArray *newArray = [[_statusArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"_"];
40         cell.textLabel.text = [newArray firstObject];
41         cell.detailTextLabel.text = [newArray lastObject];
42     return cell;
43     
44 }

运行效果:WarriorAdded Scene  |   WarriorStatus Scene

  

4 - 在开发过程中,如果推出一个新页面却不传回任何数据就回到上级页面的话,那它也就没什么实际意义。下面利用代理代理器来完成反向传值功能 

// - WarriorStatusVC.h

 1 #import <UIKit/UIKit.h>
 2 @class WarriorStatusVC;
 3 
 4 // 声明协议
 5 @protocol WarriorStatusDelegate <NSObject>
 6 
 7 - (void)warriorStatus:(WarriorStatusVC *)controller didSelectName:(NSString *)name withCapacity:(NSString *)fcValue;
 8 
 9 @end
10 @interface WarriorStatusVC : UITableViewController
11 
12 @property(nonatomic,weak)id <WarriorStatusDelegate> delegate;// 代理
13 @property(nonatomic,strong)NSString *name;
14 
15 @end

// - WarriorStatusVC.m

 1 #import "WarriorStatusVC.h"
 2 #import "Warrior.h"
 3 @implementation WarriorStatusVC{
 4     
 5     NSArray *_statusArray;
 6     NSUInteger _selectedIndex; // 已选下标
 7 }
 8 
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12 
13     _statusArray = [NSArray arrayWithObjects:
14                    [NSString stringWithFormat:@"%@_%d",@"フリーザ",FC_Base],
15                    [NSString stringWithFormat:@"%@_%d",@"ベビー",FC_Base],
16                    [NSString stringWithFormat:@"%@_%d",@"セル",FC_Base],
17                    [NSString stringWithFormat:@"%@_%d",@"ブウ",FC_Base],
18                    [NSString stringWithFormat:@"%@_%d",@"ラズリ",FC_Base],
19                    [NSString stringWithFormat:@"%@_%d",@"ブロリー",FC_Base],nil];
20     
21     
22     // 简单判断 self.name
23     // 如果不为空且 _statusArray 则检查数组中是否包含该值
24     // 如果包含就获取其下标
25     if (self.name.length !=0) {
26 
27         int i = 0; // 下标
28         for (NSString *str_i in _statusArray) {
29             i ++;
30             if ([str_i containsString:self.name]) {
31                 i--;
32                 break;
33             }
34         }
35         _selectedIndex = i;
36     }
37     
38 }
39 #pragma mark - Table view data source
40 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
41 
42     return 1;
43 }
44 
45 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
46     
47      return [_statusArray count];
48 }
49 
50 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
51     
52     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StatusCell" forIndexPath:indexPath];
53     
54     NSArray *newArray = [[_statusArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"_"];
55     cell.textLabel.text = [newArray firstObject];
56     cell.detailTextLabel.text = [newArray lastObject];
57     
58     if (self.name.length !=0) {
59         if (indexPath.row == _selectedIndex){
60             cell.accessoryType = UITableViewCellAccessoryCheckmark;// 选中
61         }else{
62             cell.accessoryType = UITableViewCellAccessoryNone;
63         }
64     }else{
65         cell.accessoryType = UITableViewCellAccessoryNone;
66     }
67     return cell;
68     
69 }
70 
71 // 选中行打对勾
72 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
73 
74     [tableView deselectRowAtIndexPath:indexPath animated:YES];
75     if(_selectedIndex != NSNotFound){
76         
77         UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
78         cell.accessoryType = UITableViewCellAccessoryNone;
79     }
80     _selectedIndex = indexPath.row;
81     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
82     cell.accessoryType = UITableViewCellAccessoryCheckmark;
83     NSArray *newArray = [[_statusArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"_"];
84     NSLog(@"%@",newArray);
85     [self.delegate warriorStatus:self didSelectName:[newArray firstObject] withCapacity:[newArray lastObject]];
86 }
87 
88 @end

// - WarriorAddedVC.m:接受 WarriorStatusDelegate 协议并实现其代理方法

1 #import "WarriorAddedVC.h"
2 #import "Warrior.h"
3 #import "WarriorStatusVC.h"
4 @interface WarriorAddedVC ()<WarriorStatusDelegate>
5 
6 @end
 1 // prepareForSegue
 2 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
 3 
 4     if ([segue.identifier isEqualToString:@"DetailsStatus"]){
 5         WarriorStatusVC *wsVC = segue.destinationViewController;
 6         wsVC.name = self.inputName.text;// 属性传值
 7         wsVC.delegate = self;
 8     }
 9 }
10 
11 // 实现代理
12 -(void)warriorStatus:(WarriorStatusVC *)controller didSelectName:(NSString *)name withCapacity:(NSString *)fcValue{
13     NSLog(@"name = %@,fcValue = %@",name,fcValue);
14     self.inputName.text = name;
15     self.inputAge.text = fcValue;
16 }

运行效果:进入 WarriorStatus 页面  |  选择倒数第二行返回上级页面并传值  |  再次进入 WarriorStatus 页面:选中效果

    

 

posted on 2017-11-03 20:41  低头捡石頭  阅读(50)  评论(0编辑  收藏  举报

导航