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 }

那麼就會出現像下面這樣的

相關文章
相關標籤/搜索