单击选项卡后,相机/相册不会在 ios 上重新加载
Posted
技术标签:
【中文标题】单击选项卡后,相机/相册不会在 ios 上重新加载【英文标题】:camera / photo album doesn't reload on ios after the tab has been clicked once 【发布时间】:2014-05-17 06:39:05 【问题描述】:我有一个 ios 应用程序,它有一个用于打开相机的选项卡按钮和另一个用于打开相册的按钮。它们第一次打开还不错,但如果您取消或转到另一个选项卡,相机和相册将无法第二次打开。
例如,这是相册视图控制器。我唯一改变的是viewDidLoad
方法和添加openPhotoAlbum
方法。我使用故事板添加标签。
PhotoAlbumViewController.m
#import "PhotoAlbumViewController.h"
#import "AppDelegate.h"
@interface PhotoAlbumViewController ()
@property (nonatomic, strong) AppDelegate *appDelegate;
@end
@implementation PhotoAlbumViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[self openPhotoAlbum];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
*/
- (IBAction)openPhotoAlbum
UIImagePickerController *albumPicker = [[UIImagePickerController alloc] init];
albumPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
albumPicker.delegate = self;
[self presentViewController:albumPicker animated:YES completion:nil];
@end
第 1 步:我点击相册
第 2 步:第一次显示相册
第 3 步:取消
第 4 步:相册不显示
【问题讨论】:
【参考方案1】:使用标签栏控制器管理您的层次结构,您的视图控制器将在您第一次选择它们时创建。当它们被创建时,viewDidLoad
将被调用。
创建后,它们会一直存在,并在您再次选择其选项卡时重新出现。
更好的选择是使用viewWillAppear
,它在第一次创建(viewDidLoad 之后)和UIViewController
的每次后续出现时都会调用。
- (void)viewDidLoad
//called only on initial creation and appearance
[super viewDidLoad];
// Do any additional setup after loading the view.
- (void)viewWillAppear:(BOOL)animated
//called on every appearance
[super viewWillAppear:animated];
[self openPhotoAlbum];
Here is a good SO answer了解UIViewController的生命周期
【讨论】:
以上是关于单击选项卡后,相机/相册不会在 ios 上重新加载的主要内容,如果未能解决你的问题,请参考以下文章