ios - 当应用程序从后台带到前台时如何制作 UIAlertView

标签 ios objective-c iphone

目前我在 viewDidLoad 上显示一个 UIAlertView,但是一旦它进入后台并重新启动,它就不会再次出现。我该如何解决这个问题?我需要哪些代表,我该怎么做?

- (void)viewDidLoad {
    [super viewDidLoad];
    UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"This is an Alert!" delegate:nil cancelButtonTitle:@"Cancel!" otherButtonTitles:nil, nil];
    [myAlert show];
}

最佳答案

苹果文档:UIApplicationDidBecomeActiveNotification

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod) name:UIApplicationDidBecomeActiveNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // don't forget remove it
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)yourMethod
{
    UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"This is an Alert!" delegate:nil cancelButtonTitle:@"Cancel!" otherButtonTitles:nil, nil];
    [myAlert show];
}

关于ios - 当应用程序从后台带到前台时如何制作 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153976/

相关文章:

ios - 自定义应用程序默认 NSDictionary

objective-c - "Append"拖动期间添加/加号图标到 NSImage

ios - 检查 key 是否存在于 NSDictionary 中

ios - 随机着色UIButton如何避免文本(黑色)和背景(白色)颜色?

objective-c - UIApplicationDelegate:其他应用程序是否有任何对Annotation的使用记录?

ios - Apple 的 On Demand Resource System 如何将文件下载到 App 的 Main Bundle 中?

objective-c - 超时在 NSAppleScript 中没有效果

iphone - 在 iTunes Connect 中,为什么分发证书已经存在?

iphone - 从表和 sqlite 数据库中删除行

iphone - 如何在 Objective-C 中使用 UIWebView 来显示网页