ios - 当 Storyboard上的 UITableViewController 单击 iOS 时显示 UIAlertView 或任何对话进度

标签 ios ios7 uitableview uialertview mbprogresshud

我有一个 Storyboard,其中有一个导航 Controller 和一个 uiviewcontroller 作为我的根,在此之后我有几个 uitableviewcontrollers 通过单元格点击上的 push seague 连接。

我需要的是在关闭当前 Controller 并显示新 Controller 时显示 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];
}

但是警报会一直显示到下一个 Controller 出现,我如何在单元格上执行点击时显示警报?

最佳答案

我找到的解决方案:

准备工作:

  1. 让你的 Controller 符合协议(protocol)UIAlertViewDelegate .

    例子:
    @interface YourViewController : UITableViewController

    @interface YourViewController : UITableViewController <UIAlertViewDelegate> .

  2. 在 Storyboard中,设置 segue 标识符。对于这个例子,我称之为CellSegue
    (为此,单击 Storyboard中的 segue,转到 Attributes Inspector,然后有 Identifier 字段)

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


编码:

下面的方法,显示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 处获取单元格来自 _selectedCellTableView ,
  2. 我们执行 CellSegue segue 并将单元格设置为发件人。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    //1
    UITableViewCell *cell =[_selectedCellTableView cellForRowAtIndexPath:_selectedCellIndexPath];

    //2
    [self performSegueWithIdentifier:@"CellSegue" sender:cell];

}

我希望这就是您要找的:)

关于ios - 当 Storyboard上的 UITableViewController 单击 iOS 时显示 UIAlertView 或任何对话进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141308/

相关文章:

android - iOS 和 Android 上的 LLVM 版本

iOS7 风格的 push segue,从左到右

ios - SVProgressHUD 在模态视图的背景中显示

ios - iOS 7完整的UINavigationBar透明度

ios - 如何在 TableView 中的可变数组中存储多个选定的行项

ios - 将 CLLocationCoordinate2D 发送到不兼容类型的参数我在 Xcode 4.5 中的代码

iphone - Objective-C 属性规则

ios - 滚动表格 View 时,自定义 UITableView 页脚不会锁定在底部

ios - filterContentForSearchText 不在 UITableView 上显示数据

ios - 以编程方式设置约束不适用于我的 TableView