UIImagePickerController 未在 iPhone SDK 的 viewDidLoad 中加载

发布于 2024-08-02 07:16:33 字数 643 浏览 5 评论 0原文

我试图在我的视图控制器之一加载后立即显示 UIImagePickerController 。我希望用户不必按下按钮,因此我重写了 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];
}

它会编译并运行,但是当加载视图控制器时,不会显示图像选择器。例如,如果我将其附加到按钮的事件,则该代码可以正常工作。有任何想法吗?

谢谢。

I'm trying to show a UIImagePickerController as soon as one of my view controller loads. I'd like to this without the user having to press a button so I overrode the viewDidLoad method as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆之渊 2024-08-09 07:16:33

我有同样的问题但解决了。尝试使用

-(void) awakeFromNib {

}

它会在其他所有内容加载后加载。

I had the same problem but solved it. Try using

-(void) awakeFromNib {

}

It will load just after everything else loads.

帅气尐潴 2024-08-09 07:16:33

尝试将代码放入

-(void)viewDidAppear

That Even 中,每次视图出现在屏幕上时都会运行(包括在您关闭 UIImagePicker 后出现时),因此您可能需要添加一个 BOOL 值以使其仅在第一次显示时发生,或者当您需要时(即不在关闭模态视图之后)。

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 (i.e. not after dismissing a modal view).

养猫人 2024-08-09 07:16:33

看来viewDidLoad使用presentModalViewController:animated:还为时过早。我建议分叉一次性计时器以从下一次运行循环迭代中调用该方法:

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

添加以下方法:

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

It seems that viewDidLoad is too early to use 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文