UIAlertViewDelegate 的使用簡介


---------------"編輯"模式: 更改名字 

// 彈出alerView
   
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"編輯"
                                                      
message:@""
                                                     
delegate:self
                                            
cancelButtonTitle:@"取消"
                                            
otherButtonTitles:@"確定", nil];
    // 設置alertView style
   
/**
     UIAlertViewStyleDefault = 0,
     UIAlertViewStyleSecureTextInput,
安全輸入
     UIAlertViewStylePlainTextInput, 
文本輸入
     UIAlertViewStyleLoginAndPasswordInput 
登錄
     */
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput;// 文本輸入
  
   
// 1. 獲取當前被點擊cell 所對應的 模型
   
HeroModel *model = self.dataArray[indexPath.row];
   
   
// 2. 把英雄的名字, 顯示到 textField
   
   
// 獲取 alertView 指定位置   textFiled
   
UITextField *textFiled = [alerView textFieldAtIndex:0];
   
    textFiled.
text = model.name;
   
   
// 設置當前aletView tag   indexPath.row
    alerView.
tag = indexPath.row;
   
    [alerView
show];
    
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   
   
if (buttonIndex == 1) { // 點擊了確定按鈕
       
       
// 1. 獲取alertView 輸入框中的內容
       
UITextField *textField = [alertView textFieldAtIndex:0];
       
       
NSString *newName = textField.text;
       
       
// 2. 刷新數據
       
// 2.1 修改數據源中 被點擊cell 對應的 模型對象中 name 修改爲  newName
       
// 先取出 alertView tag
       
NSInteger index  = alertView.tag;
       
       
// 取出index 對應到數組中的元素
       
HeroModel *model = self.dataArray[index];
       
        model.
name = newName;
       
       
// 2.2 刷新數據
       
// 這個刷新所有數據
//        [_tableView reloadData];
       
// 刷新指定cell 數據
       
// 創建NSIndexPath
       
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
       
       
/**
         AtIndexPaths:
傳遞 一個數組 數組中包含  NSIndexPath
         withRowAnimation :
動畫效果
         */

        [
_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
    }
}



---------------"編輯"模式: 更改名字 

pragma mark -  點擊cell的時候調用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
   
// 0. 取出當前cell對應的model
   
HeroModel *model = self.dataArray[indexPath.row];
   
   
// 使用alertController
   
// 1. 實例化一個alertController
   
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Edit" message:nil preferredStyle:UIAlertControllerStyleAlert];
   
   
// 2. 添加文本輸入框
    [alertController
addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
       
        textField.
text = model.name;
       
    }];
   
   
   
// 3. 添加action
   
// 3. 添加 cacel action
   
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
   
   
// 添加 cancel action alertControler
    [alertController
addAction:cancelAction];
   
   
// 4. 添加sureAction
   
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
       
// 獲取到修改後的文本
       
UITextField *field = [alertController textFields].lastObject;
       
       
NSString *newName = field.text;
       
       
// 修改數據源中的 模型 對象中的 name屬性
        model.
name = newName;
       
       
// 刷新數據
        [
_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
    }];
   
   
// sureAction 添加到控制器
    [alertController
addAction:sureAction];
   
   
   
   
// 展示alertCOntroller
    [
self presentViewController:alertController animated:YES completion:nil];
 
   
}


// 隱藏狀態欄
- (
BOOL)prefersStatusBarHidden {
   
return YES;
}

發佈了37 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章