当用户首次打开应用时,无法启动警报。我希望每个视图都有不同的警报,以指导用户完成首次运行。
我无法弄清楚我错过了什么。浏览了网站上的各种帖子,无法弄明白。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewdDidLoad {
[super viewDidLoad];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"AlreadyRan"] )
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"The Key"
message:@"Press The Key Hole"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"AlreadyRan"];
}
return 0;
}
@end
另外在另一个视图控制器上,我希望用户能够在第一次运行应用程序时通过警报弹出窗口设置密码。这是我将密码设置为标准密码的代码。密码不需要存储到钥匙串中,我不希望应用程序在本地保存密码。
PassViewController.m
- (IBAction)enterPassword {
NSString *passwordString = [NSString stringWithFormat:@"1234"];
if ([passwordField.text isEqualToString:passwordString]) {
//Password is Correct
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"PhotoView"];
[self presentViewController:vc animated:YES completion:nil];
}
else {
//Password is wrong
[self dismissViewControllerAnimated:YES completion: nil];
}
}
答案 0 :(得分:1)
您已在视图控制器中实现了(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
,但实际上此方法属于UIApplicationDelegate
协议(通常位于AppDelegate.m中)。
因此iOS永远不会调用此方法。
我认为您正在寻找的是-(void) viewDidAppear: (BOOL) animated { ... }
。当视图控制器出现在屏幕上时会调用它。所以你的实现看起来像
-(void) viewDidAppear: (BOOL) animated {
[super viewDidAppear: animated];
/* All your other code that used to be in didFinishLaunchingWithOptions */
}