我需要让UIAlertView阻塞。因为我有函数,并且我需要返回UIAlertView选择。但问题是,在显示UIAlertView之后,我的函数代码将进一步执行,因此我无法捕获UIAlertView选择(我可以在委托方法中执行,但我需要返回函数结果)。
我试着用NSCondition来阻止UIAlertVIew。但是代码不起作用。
condition = [NSCondition new];
result = 0 ;
[condition lock];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fingerprint" message:@"test" delegate:window_self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[alert setDelegate:self];
[alert show];
while (result == 0) [condition wait];
[condition unlock] ;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[condition lock] ;
if (buttonIndex == 0)
{
result = 2;
}
else if (buttonIndex == 1)
{
result = 3 ;
}
[condition signal] ;
[condition unlock] ;
}也许如何修复此代码或任何其他建议?谢谢
发布于 2010-09-21 17:18:02
没有办法实现你想要的。只能通过委派。你应该重新设计你的函数或者拒绝使用UIAlertView
发布于 2013-05-10 21:18:00
这并不会使其阻塞,但我已经编写了一个子类来添加块样式语法,如果在一个类中有多个UIAlertViews,则可以更容易地处理buttonClickedAtIndex方法,而不必执行委托和一大堆if语句。
#import <UIKit/UIKit.h>
@interface UIAlertViewBlock : UIAlertView<UIAlertViewDelegate>
- (id) initWithTitle:(NSString *)title message:(NSString *)message block: (void (^)(NSInteger buttonIndex))block
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_AVAILABLE(10_6, 4_0);
@end
#import "UIAlertViewBlock.h"
@interface UIAlertViewBlock()
{
void (^_block)(NSInteger);
}
@end
@implementation UIAlertViewBlock
- (id) initWithTitle:(NSString *)title message:(NSString *)message block: (void (^)(NSInteger buttonIndex))block
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_AVAILABLE(10_6, 4_0)
{
if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil])
{
_block = block;
}
return self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
_block(buttonIndex);
}
@end然后在这里调用它是一些示例代码。另一个很酷的部分是,因为块关闭了局部变量,所以我可以访问在显示UIAlertView时存在的所有状态。使用传统的委托方法,您必须将所有临时状态存储到类级别变量中,才能在委托中调用buttonClickedAtIndex时访问它。这里干净多了。
{
NSString *value = @"some random value";
UIAlertViewBlock *b = [[UIAlertViewBlock alloc] initWithTitle:@"Title" message:@"Message" block:^(NSInteger buttonIndex)
{
if (buttonIndex == 0)
NSLog(@"%@", [value stringByAppendingString: @" Cancel pressed"]);
else if (buttonIndex == 1)
NSLog(@"Other pressed");
else
NSLog(@"Something else pressed");
}
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other", nil];
[b show];
}发布于 2011-12-16 06:06:11
我正面临着同样的问题。虽然没有解决方案,但至少有两个我想到的变通方法。
在调用UIAlert之后,立即循环“解决方案”,然后开始一个循环,查找对象(注意,不是整个项目)的全局变量中的变化,该变量是您在接受答案的UIAlert委托中设置的变量。所以基本上你需要等待"is A == 1,if not DoEvents“,然后在上面循环。
然后,在得到答案的情况下,在委托上设置A=1
在有人说可可没有DoEvents之前:
void MyTestClass::DoEvents()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event) {
[NSApp sendEvent:event];
[NSApp updateWindows];
}
[pool release];
}委托解决方案,而不是在调用Alert的函数中处理答案A、B或C的代码,将代码放在委托本身中。
希望能有所帮助。我在我的项目中使用了第二个,它起作用了。
https://stackoverflow.com/questions/3753154
复制相似问题