我有一个 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 出现,我如何在单元格上执行点击时显示警报?
最佳答案
我找到的解决方案:
准备工作:
让你的 Controller 符合协议(protocol)
UIAlertViewDelegate
.
例子:
@interface YourViewController : UITableViewController
到
@interface YourViewController : UITableViewController <UIAlertViewDelegate>
.在 Storyboard中,设置 segue 标识符。对于这个例子,我称之为CellSegue。
(为此,单击 Storyboard中的 segue,转到 Attributes Inspector,然后有 Identifier 字段)您将需要 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
我们处理按钮点击的方法:
- 我们在
_selectedCellIndexPath
处获取单元格来自_selectedCellTableView
, - 我们执行 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/