覆盖 alertView 方法
Override alertView method
我有一个带按钮的单元格。此按钮用于删除单元格,当我触摸按钮时,我会在删除前显示一个 alertView。我想要的是将参数中的单元格传递给如下方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex forCell:(MissionCollectionViewCell *) cell
但我不知道在 UIAlertView 初始化期间该怎么做?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"Etes-vous sûr de vouloir supprimer cette mission ?" delegate:self cancelButtonTitle:@"Supprimer" otherButtonTitles:@"Annuler", nil];
如果您只想支持 iOS8+,您应该转而使用 UIAlertController,它为您提供基于块的操作处理程序,从而轻松访问变量。如果像我们中的许多人一样,您需要满足那些不立即升级的需求,我发现 here 描述的方法是处理 UIAlertView 的最佳方式。
该示例 link 向 UIAlertView 添加了一个类别,以允许您使用块而不是委托来处理警报视图完成。使用您的示例,它会让您发出警报并在完成时让您访问您需要的任何变量:
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Attention"
message:@"Etes-vous sûr de vouloir supprimer cette mission ?"
delegate:self
cancelButtonTitle:@"Supprimer" otherButtonTitles:@"Annuler", nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
// Since this is a block defined where you called it you can use
// the variables you want directly 8^).
if (buttonIndex == alertView.cancelButtonIndex){
... Code can access cell directly.
}
}];
我现在一直在使用它,因为将完成代码放在引发警报视图的位置旁边会简单得多。
我有一个带按钮的单元格。此按钮用于删除单元格,当我触摸按钮时,我会在删除前显示一个 alertView。我想要的是将参数中的单元格传递给如下方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex forCell:(MissionCollectionViewCell *) cell
但我不知道在 UIAlertView 初始化期间该怎么做?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"Etes-vous sûr de vouloir supprimer cette mission ?" delegate:self cancelButtonTitle:@"Supprimer" otherButtonTitles:@"Annuler", nil];
如果您只想支持 iOS8+,您应该转而使用 UIAlertController,它为您提供基于块的操作处理程序,从而轻松访问变量。如果像我们中的许多人一样,您需要满足那些不立即升级的需求,我发现 here 描述的方法是处理 UIAlertView 的最佳方式。
该示例 link 向 UIAlertView 添加了一个类别,以允许您使用块而不是委托来处理警报视图完成。使用您的示例,它会让您发出警报并在完成时让您访问您需要的任何变量:
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Attention"
message:@"Etes-vous sûr de vouloir supprimer cette mission ?"
delegate:self
cancelButtonTitle:@"Supprimer" otherButtonTitles:@"Annuler", nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
// Since this is a block defined where you called it you can use
// the variables you want directly 8^).
if (buttonIndex == alertView.cancelButtonIndex){
... Code can access cell directly.
}
}];
我现在一直在使用它,因为将完成代码放在引发警报视图的位置旁边会简单得多。