閱讀274 返回首頁    go 汽車大全


IOS之Block的應用-textFeild的回調應用

Block的一點優點為可以省略回調函數,簡化代碼今天我就應用了以下。

以下是代碼片段。

    _testTextField1=[[MyTextField alloc] init];
    [self.view addSubview:_testTextField1];
    [_testTextField1 release];
    _testTextField1.obj=self;
    _testTextField1.textField.placeholder=@"小於5的字符判斷";
    _testTextField1.textField.font=[UIFont systemFontOfSize:14];
    _testTextField1.myConditionBlock=^(NSString * str){//加入你要判斷或者賦值的語句
        if(str.length<=5)
        {
        
            return YES;
        }
        return NO;};
    _testTextField1.myAlertBolck=^(id obj){
        [_testTextField1.textField resignFirstResponder];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"test" message:@"請輸入小於5的字符串" delegate:obj cancelButtonTitle:@"確定" otherButtonTitles: nil];
        [alertView show];
        [alertView release  ];
    };

這樣可以把判斷條件和觸發事件分開,並且放在一個實例裏麵。並且可以把兩個實例相關聯。使用起來較為方便。

以下是代碼的下載地址:https://download.csdn.net/detail/matrixhero/4645105


最後更新:2017-04-02 15:15:05

  上一篇:go 黑客道德準則 不是所有的黑客都是罪犯
  下一篇:go 固態盤的集成與災難恢複的維護