
记一次解决跨控制器监听开关状态改变的尝试。
为了统一设置UITableViewCell里的内容,自定义了UITableViewCell类的一个基类,命名为SettingCell。SettingCell里显示的内容由数据模型SettingItem提供:在SettingCell里定义一个属性即可。
@property (nonatomic, strong) SettingItem *item;
再定义几个SettingItem的子类表示显示不同内容的Cell(如图1).由于所有开关状态的归档和解档都一样,故统一在父类SettingItem里实现。但是点击“手势密码”后跳转到下一个控制(如图2)后,需要单独监听手势密码开关的状态,这就涉及到了跨控制器监听开关状态改变事件的问题。我首先想到了代理模式。
图1 图2
首先在SettingCell.h里声明代理方法和属性:
#import <UIKit/UIKit.h> @class SettingItem, SettingCell; @protocol SettingCellDelegate <NSObject> @optional // 监听开关状态改变
- (void)settingCell:(SettingCell *)cell switchChanged:(UISwitch *)switchView; @end @interface SettingCell : UITableViewCell // 存放模型数据
@property (nonatomic, strong) SettingItem *item; @property (nonatomic, assign, getter = isLastRowInSection) BOOL lastRowInSection; + (instancetype)cellWithTableView:(UITableView *)tableView; // 定义代理属性
@property (nonatomic, weak) id<SettingCellDelegate> delegate; @end
然后,在SettingCell.m里初始化开关并注册ValueChanged事件,在switchStateChange:方法里调用代理方法传递开关状态:
- (UISwitch *)switchView
{
if (_switchView == nil) {
_switchView = [[UISwitch alloc] init];
[_switchView addTarget:self action:@selector(switchStateChange:) forControlEvents:UIControlEventValueChanged];
}
return _switchView;
} /**
* 监听开关状态改变
*/
- (void)switchStateChange:(UISwitch *)switchView
{
// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// [defaults setBool:self.switchView.isOn forKey:self.item.title];
// [defaults synchronize];
if ([self.delegate respondsToSelector:@selector(settingCell:switchChanged:)]) {
[self.delegate settingCell:self switchChanged:switchView];
} [CoreArchive setBool:self.switchView.isOn key:self.item.title];
}
最后,在图2的控制器里实现代理方法:
- (void)settingCell:(SettingCell *)cell switchChanged:(UISwitch *)switchView
{
NSLog(@"手势密码开关状态改变了-------------------");
}
但是发现点击开关后并没有发现打印结果,窘迫~
检查代理模式的使用方法后发现是没有设置SettingCell的代理为当前控制器(GestureViewController)。问题又来了,在图2的控制器里我根本拿不到SettingCell,无奈只好追本溯源,发现这个控制器又是继承自自定义的一个控制器(BaseSettingViewController),故在父类控制器里的Cell初始化方法里设置代理为当前控制器即可(下面代码第11行)。于是开心地看到打印结果鸟!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
SettingCell *cell = [SettingCell cellWithTableView:tableView]; // 2.给cell传递模型数据
SettingGroup *group = self.data[indexPath.section];
cell.item = group.items[indexPath.row];
cell.lastRowInSection = (group.items.count - == indexPath.row);
// 设置代理
cell.delegate = self;
// 3.返回cell
return cell;
}
总结思路:
- 涉及到跨控制器数据访问时首先考虑代理模式;
- 当类的继承关系复杂时一定要缕清关系:什么事在基类里统一做,什么事在子类里单独做。