每次单击特定选项卡时如何使相机出现?

时间:2012-03-24 10:40:47

标签: iphone ios5 uistoryboard xcode4.3

info:xcode 4.3.2,iOS5,使用故事板。

从xcode" Tabbed Application"创建项目模板。
做的:

@implementation SUSecondViewController
UIImagePickerController *pic;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    pic = [[UIImagePickerController alloc] init];
    pic.delegate = self;
   // [pic setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentModalViewController:pic animated:YES];
}

第一次点击标签时,会显示相机应用程序 当我第二次单击该选项卡时,将显示模板中的默认视图,但是我希望每次单击选项卡时都显示相机。

每次点击特定标签(带有f.ex.相机图标)时,如何让相机出现?

2 个答案:

答案 0 :(得分:2)

viewDidLoad仅在加载视图时发生。

如果视图消失/重新出现,则无法保证视图将被卸载/加载。 也许是因为某个地方仍然有一个指向viewController的强指针,系统不需要释放一些内存。

如果您想在每次出现视图时调用它,请将该代码放在UIViewControllers中

- (void)viewDidAppear

方法而不是

- (void)viewDidLoad

您可能还想看看这里: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW1

答案 1 :(得分:2)

viewDidAppear:viewDidLoad使用它,因为在viewDidLoad执行完毕之前Nib未完全加载,您可能无法获得与UI相关的内容。

所以我希望您在viewDidAppear:上对此进行编码,如下所示

@implementation SUSecondViewController
UIImagePickerController *pic;

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // Do any additional setup after loading the view, typically from a nib.
    pic = [[UIImagePickerController alloc] init];
    pic.delegate = self;
   // [pic setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentModalViewController:pic animated:YES];
}

希望这会对你有所帮助。