我需要UIAlertView阻止.因为我有功能,我需要返回UIAlertView选择.但问题是显示后UIAlertView我的功能代码进一步执行所以我无法UIAlertView选择(我可以在委托方法中执行,但我需要返回函数结果).
我试图UIAlertVIew阻止NSCondition.但代码不起作用.
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] ;
}
Run Code Online (Sandbox Code Playgroud)
也许如何修复此代码或任何其他建议?谢谢
小智 5
这不会阻止它,但是我已经编写了一个子类来添加块样式语法,如果你在一个类中有多个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
Run Code Online (Sandbox Code Playgroud)
然后在这里调用它是一些示例代码.另一个很酷的部分是,因为一个块关闭局部变量,我可以访问我显示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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7400 次 |
| 最近记录: |