gpt4 book ai didi

objective-c - 在 didFinishLaunchingWithOptions 中连续两个 UIAlertView

转载 作者:行者123 更新时间:2023-12-01 17:59:53 24 4
gpt4 key购买 nike

我希望仅在用户第一次打开我的应用程序时才显示两个警报 View ——第二个在第一个被关闭后出现。我将它设置为仅在以前未显示 UIAlertViews 时才显示它,我不需要帮助。在这种情况下,我需要帮助弄清楚如何连续显示两个警报 View 。

-(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
}

最佳答案

我将使用 GCD & blocks 发布一个非常简单的解决方案(GCD 部分是为了防止警报 View 是在另一个线程上创建的,然后是主线程,回调应该可以安全地在主线程上执行)。请记住,我只是在 5 分钟内编写了此代码,因此您绝对应该努力改进代码。有点难看的一件事是在我的子类中覆盖的委托(delegate)参数。子类的接口(interface)可以稍微改变一下,以使发生的事情更加明显......

无论如何,这里...

首先创建 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);
}

});
}

现在在应用程序中,我们可以创建警报 View ,如下所示...
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];
}

}];

关于objective-c - 在 didFinishLaunchingWithOptions 中连续两个 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com