Objective c 为什么UIAlertView在用户通过点击按钮做出响应之前不会阻塞?
在我的iOS 7应用程序中,我需要验证用户是否希望从数据线删除选定的记录。我在.h文件中定义了UIAlertViewDelegate。这是显示警报的代码:Objective c 为什么UIAlertView在用户通过点击按钮做出响应之前不会阻塞?,objective-c,ios7,uialertview,uialertviewdelegate,Objective C,Ios7,Uialertview,Uialertviewdelegate,在我的iOS 7应用程序中,我需要验证用户是否希望从数据线删除选定的记录。我在.h文件中定义了UIAlertViewDelegate。这是显示警报的代码: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Are you sure you want to delete this record?"
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure you want to delete this record?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
[alert show];
if(alertButtonTapped == 0)
return;
// remainder of code to delete record follows (was omitted)
这是检查点击哪个按钮的代码:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
alertButtonTapped = [NSNumber numberWithInteger:buttonIndex];
return;
}
问题在于,警报显示后,该方法中的其余代码会立即消失。我以前从未见过这种情况;通常它会一直阻塞,直到用户点击其中一个按钮做出响应(至少我认为是这样)。我需要做什么才能阻止此警报,直到用户响应?(我正在查看UIAlertView块,但不确定这是否可以完成任务,因为它似乎使用了另一个线程)这就是
UIAlertView
的工作原理——它没有块,因此它有UIAlertViewDelegate
方法来实际实现响应
如果“代码的剩余部分”是他们点击按钮(例如“删除”按钮)后应该发生的事情,则将所有代码移动到委托方法中,如:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != alertView.cancelButtonIndex) {
// code to delete record
}
}
编辑-添加示例以回答评论
因此,如果在同一类中有多个UIAlertView
s,则可以使用UIView
的标记
属性(UIAlertView
is-aUIView
)来区分它们。所以可能是这样的:
const NSInteger kDeleteAlertTag = 100; // declared at the top of your .m file, perhaps.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure you want to delete this record?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
alert.tag = kDeleteAlertTag;
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == kDeleteAlertTag) {
if (buttonIndex != alertView.cancelButtonIndex) {
// code to delete record
}
}
else if (alertView.tag = kDoSomethingElseAlertTag) {
DoSomethingElse();
}
}
那么您的代理响应可能如下所示:
const NSInteger kDeleteAlertTag = 100; // declared at the top of your .m file, perhaps.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Are you sure you want to delete this record?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Delete", nil];
alert.tag = kDeleteAlertTag;
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == kDeleteAlertTag) {
if (buttonIndex != alertView.cancelButtonIndex) {
// code to delete record
}
}
else if (alertView.tag = kDoSomethingElseAlertTag) {
DoSomethingElse();
}
}
如何区分同一类中的几个UIAlertView?有几种可能的方法。不必将alloc/init的UIAlertView存储在局部变量中,您可以将其存储为类数据成员,然后将其与传递到委托方法中的“alertView”参数进行比较,以确定它是否是同一个对象。这很有效,但相当严厉。更轻松的方法是利用UIView的“tag”属性(UIAlertView就是其中之一)。我将在上面添加一个插图。明白了。。。此外,还有一些第三方AlertView()使用块,看起来更干净。谢谢你抽出时间。。。我很感激,是的。这样的扩展(我喜欢BlocksKit)也非常有用,并且可以通过这种方式解决问题。不客气,祝您愉快。:-)