问题制作方法在应用程序启动时运行

时间:2014-04-23 22:25:23

标签: ios objective-c cocoa-touch camera

我正致力于开发一款将相机用于各种不同用途的应用。现在,当我启动应用程序时,我正在努力获得一些代码:

UIImagePickerController *imageView = [[UIImagePickerController alloc]init];
imageView.delegate = self;
imageView.sourceType = UIImagePickerControllerSourceTypeCamera;
imageView.showsCameraControls = NO;

[self presentViewController:imageView animated:YES completion:NULL];

我需要在启动时在imageView UIView对象上执行此操作,因此当应用程序打开时,它会直接转到UIImagePickerController。以下是我的应用程序的所有代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)applicationDidBecomeActive
{

    UIImagePickerController *imageView = [[UIImagePickerController alloc]init];
    imageView.delegate = self;
    imageView.sourceType = UIImagePickerControllerSourceTypeCamera;
    imageView.showsCameraControls = NO;

    [self presentViewController:imageView animated:YES completion:NULL];


}

@end

2 个答案:

答案 0 :(得分:0)

如果您希望在启动应用程序时运行代码,则应将其放入:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

如果你想让UIImagePickerController成为第一个提供的viewController,你应该将它设置为窗口的rootViewController,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Set up imageView here

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = imageView;
    return YES;
}

如果/当您想要从已经在屏幕上的另一个viewController呈现它时,请使用presentViewController。

答案 1 :(得分:0)

如果您希望在任何控制器中收到通知,您应该听取UIApplicationDidBecomeActiveNotificationsimilar notifications

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBecomeActive)
                                                 name:UIApplicationDidBecomeActiveNotification 
                                               object:nil];
}

取消注册,执行

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];

dealloc或任何您不想再收到通知的时候。


如果您使用故事板,您的应用代表应该是这样的

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

//…

@end 

如果您不使用故事板,您的应用代表可能

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[ViewController alloc] init];
    return YES;
}

//…

@end 

和你的视图控制器

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBecomeActive)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationDidBecomeActiveNotification
                                                  object:nil];
}

- (void)applicationDidBecomeActive
{

    UIImagePickerController *imageViewPickerController = [[UIImagePickerController alloc] init];
    imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imageViewPickerController
                       animated:NO
                     completion:NULL];
}

@end