IOS工做笔记(十)

1.关于textField数组

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

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

 

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

 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.对于代理和赋值方法的处理。spa

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

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

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

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

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

 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的设立。server

 

4.@selector方法能够定义,而后直接使用blog

对于没参数的继承

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 }

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

相关文章
相关标签/搜索