当故事板上的UITableViewController单击iOS时,显示UIAlertView或任何对话进度

时间:2014-04-17 18:59:08

标签: ios ios7 uitableview uialertview mbprogresshud

我有一个带有导航控制器和uiviewcontroller作为我的根的故事板,在此之后我有几个uitableviewcontrollers通过单击点击按下连接。

我需要的是显示UIAlertView或一些进度对话框(如MBProgressHUD),同时解除当前控制器并显示新控制器。

我尝试在点击单元格上设置UIAlertView:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"I am dismissing"
                                       message:nil
                                      delegate:nil
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];
    [alert show];
}

但警报会显示,直到下一个控制器出现,如何在单元格上执行点击时显示警告?

1 个答案:

答案 0 :(得分:1)

我找到的解决方案:

制剂

  1. 使您的控制器符合协议UIAlertViewDelegate

    示例:
    @interface YourViewController : UITableViewController

    @interface YourViewController : UITableViewController <UIAlertViewDelegate>

  2. 在故事板中,设置segue标识符。这个例子让我称之为 CellSegue 。(要这样,单击故事板中的segue,转到属性检查器并且有标识符字段)

  3. 您需要2个属性 @property (strong, nonatomic) UITableView *selectedCellTableView;
    @property (strong, nonatomic) NSIndexPath *selectedCellIndexPath;


  4. 编码:

    以下方法显示alertview并通过返回nil来阻止选择单元格。我们想要记住哪个单元格被选中,因此我们按如下方式设置之前准备的属性:

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"alert" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    
        _selectedCellTableView = tableView;
        _selectedCellIndexPath = indexPath;
    
        [av show];
    
        return nil;
    }
    

    最后,在UIAlertViewDelegate方法中我们处理按钮点击:

    1. 我们从_selectedCellIndexPath
    2. 获取_selectedCellTableView的单元格
    3. 我们执行 CellSegue segue并将单元格设置为发件人。
    4. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
      
          //1
          UITableViewCell *cell =[_selectedCellTableView cellForRowAtIndexPath:_selectedCellIndexPath];
      
          //2
          [self performSegueWithIdentifier:@"CellSegue" sender:cell];
      
      }
      

      我希望这就是你要找的东西:)