我希望只有当用户第一次打开我的应用程序时才显示两个警报视图 - 第二次出现在第一次被取消后出现。我把它设置为只显示UIAlertViews以前没有显示过,我不需要帮助。在这种情况下,我需要帮助确定如何连续显示两个警报视图。
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex :( NSInteger)buttonIndex对我不起作用。
这是我的代码 - 请记住这是在didFinishLaunchingWithOptions中:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:@"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:@"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
答案 0 :(得分:4)
我将使用GCD
& blocks
发布一个非常简单的解决方案( GCD部分是为了防止在另一个线程上创建警报视图然后主线程,回调应该可以安全地执行主线程)。请记住,我只是在5分钟内对此进行了编码,因此您肯定应该努力改进代码。有一件事有点难看的是在我的子类中重写的委托参数。子类的接口可以稍微改变一下,以便更明显地发生什么......
无论如何,这里......
首先创建UIAlertView
的子类,使其看起来有点像以下......
@interface FSAlertView () <UIAlertViewDelegate>
@property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
@end
@implementation FSAlertView
@synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
现在,在应用中,我们可以创建如下的警告视图......
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:@"Alert 1"
message:@"Some message"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(@"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Alert 2"
message:@"Hi!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
答案 1 :(得分:1)
如果我理解你的问题,那么这可能有所帮助:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:@selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:@selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
这将一个接一个地显示两个警报视图。
注意:我不确定您是否使用取消按钮解除警报,因此我会在几秒钟后自动解除警报。
答案 2 :(得分:0)
试试这个:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView委派方法:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Title 2" message:@"Message2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(@"Code Here");
}
}
break;
}
}