简体   繁体   English

UIImagePickerController没有加载到iPhone SDK的viewDidLoad中

[英]UIImagePickerController not loading in viewDidLoad for iPhone SDK

I'm trying to show a UIImagePickerController as soon as one of my view controller loads. 我正在尝试在我的一个视图控制器加载时显示UIImagePickerController。 I'd like to this without the user having to press a button so I overrode the viewDidLoad method as follows: 我想在没有用户按下按钮的情况下这样做,所以我覆盖了viewDidLoad方法,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.allowsImageEditing = YES;
    imagePickerController.delegate = self;    
    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}

This compiles and runs, however when the view controller is loaded the image picker is not displayed. 这将编译并运行,但是在加载视图控制器时,不会显示图像选择器。 This code works fine if I attach it to an event of a button for example. 如果我将它附加到按钮的事件,例如,此代码可以正常工作。 Any ideas? 有任何想法吗?

Thanks. 谢谢。

I had the same problem but solved it. 我有同样的问题,但解决了它。 Try using 尝试使用

-(void) awakeFromNib {

}

It will load just after everything else loads. 它将在其他所有加载后加载。

Try putting the code in 尝试将代码放入

-(void)viewDidAppear

That even runs every time the view appears on the screen though (including when it appears after you dismiss the UIImagePicker), so you might have to add a BOOL value to make it only happen the first time it shows, or when you want it (ie not after dismissing a modal view). 这甚至在每次视图出现在屏幕上时都会运行(包括在您关闭UIImagePicker后出现的情况),因此您可能需要添加BOOL值才能使其仅在第一次显示时或在您需要时发生(即在解雇模态视图后没有)。

It seems that viewDidLoad is too early to use presentModalViewController:animated:. 似乎viewDidLoad太早使用presentModalViewController:animated:。 I'd sugget to fork off a one-shot timer to call the method from next run loop iteration: 我建议分叉一次性计时器从下一次运行循环迭代中调用该方法:

[NSTimer
 scheduledTimerWithTimeInterval:0
 target:self
 selector:@selector(onLoadTimer:)
 userInfo:nil
 repeats:NO];

add the following method: 添加以下方法:

- (void)onLoadTimer:(id)unused
{
    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM