Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序启动时运行问题生成方法_Ios_Objective C_Cocoa Touch_Camera - Fatal编程技术网

Ios 在应用程序启动时运行问题生成方法

Ios 在应用程序启动时运行问题生成方法,ios,objective-c,cocoa-touch,camera,Ios,Objective C,Cocoa Touch,Camera,我正在开发一个应用程序,将摄像头用于各种不同的用途。现在,当我启动应用程序时,我正在努力获取一些要运行的代码: UIImagePickerController *imageView = [[UIImagePickerController alloc]init]; imageView.delegate = self; imageView.sourceType = UIImagePickerControllerSourceTypeCamera; imageView.showsCameraContro

我正在开发一个应用程序,将摄像头用于各种不同的用途。现在,当我启动应用程序时,我正在努力获取一些要运行的代码:

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

[self presentViewController:imageView animated:YES completion:NULL];
- (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
我需要在启动时在
imageView
UIView对象上执行此操作,这样当应用程序打开时,它会直接转到
UIImagePickerController
。以下是我的所有应用程序代码:

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

[self presentViewController:imageView animated:YES completion:NULL];
- (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

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

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

如果您想在任何控制器中获得通知,您应该收听
UIApplicationIDBecMeactiveNotification

注销

[[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

实际的问题是什么?你发布的代码在哪里?我编辑了我的帖子,希望它更清晰一点。谢谢你的评论。我觉得这让我非常接近,但我不太明白UIWindow对象是什么。我给了它一个谷歌,我找不到任何简明的信息。我不太理解viewController和UIWindow之间的区别。一个应用程序有一个窗口,但可以有多个viewController。在Xcode中创建项目时,它应该在AppDelegate.m文件中包含didFinishLaunchingWithOptions中的窗口代码。您只需添加rootViewController的行设置。您可能应该阅读一些文档:请观看WWDC 2011视频“实现UIViewController包容”。它解释了一点,为什么这样更改rootviewcontroller是个坏主意,实际上工程师多次警告“我向您保证,坏事情会发生”。最不可能发生的事情是方向问题。@vikingosegundo是的,他的答案可能不是这个问题的最佳答案,但我假设你认为设置rootViewController是不对的。我想我应该更清楚一些。我真的需要在应用程序打开时显示我的
UIImagePickerController
,我希望使用故事板让UI设计更简单一点。我知道按钮和其他UI元素可以通过编程方式创建,但对于这个摄像头应用程序,视觉布局更可取。那么,我可以将代码应用到这个应用程序吗?我看不出这如何使我能够启动
UIImagePickerController
。您没有看到在收到通知
UIApplicationIDBecMeactivification
时执行您的
ApplicationIDBecMeactivitive
吗?啊,是的。因此,我将代码放在AppDelegate.m中的
applicationIDBecomeActive
中?这样,当运行
UIApplicationIDBECOMEACTIVENTIfication
时,它会引用这个?