IOS工作笔记(十)

1.关于textField

在做一些操作,比如登录时账号或密码为空的处理时,有两种方案。
①登录按钮可以点击,但会用alertView提示“账号或密码为空”的消息。
②此时登录按钮不可点击,只有账号和密码都有值时,才可以点击。
两种方法推荐第二种,体验较好。此时需要实现UITextFieldDelegate,并且

1 - (BOOL)textFieldShouldClear:(UITextField *)textField{
2     //清空内容时同时使按钮不可点击
3     _saveBtn.enabled = NO;
4     return YES;
5 }
6 //该方法表示是否允许根据请求清空内容.

2.当一个view内有多个UIActionSheet时,可以对actionSheet设置不同的tag值,再根据tag值处理选项。如

 1 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
 2     switch (actionSheet.tag) {
 3         case 1:
 4             switch (buttonIndex) {
 5                 case 0:
 6                     NSLog(@"点击了0");
 7                     break;
 8                     
 9                 default:
10                     break;
11             }
12             break;
13         
14         case 2:
15             switch (buttonIndex) {
16                 case 0:
17                     NSLog(@"点击了3");
18                     break;
19                     
20                 case 1:
21                     NSLog(@"点击了4");
22                     break;
23                     
24                 default:
25                     break;
26             }
27             break;
28             
29         default:
30             break;
31     }
32 }

3.对于代理和赋值方法的处理。

现有两个方法,一个代理方法,另一个是对cell进行赋值的cellForRowAtIndexPath。

//代理方法,执行晚于cellForRowAtIndexPath,但会回调。
-(void)getData:(NSArray *)arr{
    //
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //
}

有一个数组arr,由代理方法进行赋值,然后将arr里的数据对cell赋值。但程序运行时,先执行cellForRowAtIndexPath,因此需要设置一个值,用来标记是否对arr赋值。

处理方法是设置一个BOOL值_isValued,设其初始值为NO,然后在代理方法中设为YES,再对cell进行赋值即可。

 1 //该方法比代理方法先执行
 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 3     if(_isValued){
 4         //进行赋值操作
 5     } else {
 6         //设置空白cell
 7     }
 8 }
 9 
10 -(void)getData:(NSArray *)arr{
11     //其它操作
12     //赋值完毕后,设置
13     _isValued = YES;
14 }

关键在于_isValue的设立。

4.@selector方法可以定义,然后直接使用

对于没参数的

1 SEL sel = @selector(refreshPersonDataByNotification);
2 [[NSNotificationCenter defaultCenter] addObserver:self selector:sel name:imgTextChannelNotification object:nil];
3 
4 //刷新数据
5 -(void)refreshPersonDataByNotification{
6     [_tableView refreshLoad];
7 }

有参数的可以这样写

SEL sel = @selector(write:andAge:);
-(void)write andAge:(NSString *)age{
    //
}

5.若不同的.m有多个操作,每个操作都发送一个通知,但该操作都执行同一项任务,那么在注册监听者时,无需注册多个不同名称的通知,只需一个即可。

如:

1 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:note1 object:nil];
2 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:note2 object:nil];
3 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:note3 object:nil];

完全可以合为1个,只要通知的名称统一即可。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:note object:nil];

声明通知的name时推荐使用

NSString * const AnyNotificationName = @"notiName";

因为使用下面的写法有可能出现警告:sending 'const NSString *__strong' to parameter of type 'NSString' discards qualifiers

const NSString *AnyNotificationName = @"notiName";

6.关于继承的一些回顾

子类继承父类后,子类就会带有父类的所有public性质的方法和属性。如在一个父类的UIView的.h中

 1 @interface ViewFather : UIView{    
 2 @public
 3     UILabel *_lab;
 4 }
 5 
 6 @property(nonatomic,strong) UIButton *firstBtn;
 7 @property(nonatomic,strong) UIButton *secondBtn;
 8 @property(nonatomic,strong) UIButton *thirdBtn;
 9 
10 @end

子类继承后,

@interface ViewSon : ViewFather

@end

那么可以通过self.firstBtn这样的来获取到用property修饰的属性,但若想获取_lab这样的,就只能通过

self->_lab

来获取,并且类似于

{    
@public
    UILabel *_lab;
}

这样的,若想让子类继承,只能写到父类的.h中,.m中在子类中是获取不到的。
对于方法,若方法名相同,则子类会覆盖父类中的方法。若不同,则会执行两个方法。如下:
父类中有

 1 UIButton *firBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 2 [firBtn setTitle:@"第一个" forState:UIControlStateNormal];
 3 [firBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 4 firBtn.frame = CGRectMake(100, 50, 100, 50);
 5 firBtn.layer.cornerRadius = 20;
 6 firBtn.backgroundColor = [UIColor brownColor];
 7 self.firstBtn = firBtn;
 8 [self.firstBtn addTarget:self action:@selector(alertMsg) forControlEvents:UIControlEventTouchUpInside];
 9 [self addSubview:firBtn];
10 
11 -(void)alertMsg{
12     UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"第一项",@"第二项",@"第三项", nil];
13     [sheet showInView:self];
14 }

子类中则有

1 [self.firstBtn addTarget:self action:@selector(alertMsg2) forControlEvents:UIControlEventTouchUpInside];
2 
3 -(void)alertMsg2{
4     UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"哈哈",@"嘿嘿",@"呼呼", nil];
5     [sheet showInView:self];
6 }

那么就会出现像下面这样的

原文地址:https://www.cnblogs.com/Apologize/p/4775814.html