陈开杰 陈开杰 - 4 months ago 15
iOS Question

Why do I call this method and then current VC automatically back to the rootVC

I call this method

[self dismissViewControllerAnimated:true completion: nil];


and then current VC automatically back to the rootVC.

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// 直接取消
}];
UIAlertAction *photo = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 拍照
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 从相册中选取
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
[actionSheet addAction:photo];
[actionSheet addAction:album];
[actionSheet addAction:cancel];
[self presentViewController:actionSheet animated:YES completion:nil];


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
self.user_icon.image = [info objectForKey:UIImagePickerControllerEditedImage];
NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);
// Log "<PersonViewController: 0x7f9bf485be00>"
[self dismissViewControllerAnimated:true completion: ^{
NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);
// Log (null)
}];
}


I do not know where I set up wrong, but the same code in another project not wrong , please help me solve this problem , thank you!


Latest update:


Question wrong place:

I placed UITabBarController initialization inside viewWillAppear


before code:


- (id)initWithLoginType:(LoginType)loginType{
self = [super init];
if (self) {
self.loginType = loginType;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];

}
- (void)viewWillAppear:(BOOL)animated{
[self loadTabBarControllers];
}
- (void)loadTabBarControllers{
xxxxxxxx UITabBarController initialization
}



After Code:



- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
LoginType loginType = appDelegate.loginType;
xxxxxxxx UITabBarController initialization
}



Although the problem has been solved , but I'm still not sure why UITabBarController initialization on viewWillAppear this (presentViewController and then dismissViewController. the currentVC is released, and automatically back to rootVC) happen ?

Answer

dismissViewControllerAnimated pop you back to previous view controller which was available in your navigation stack. so make sure that you are not releasing your previous viewcontrollers from navigation hierarchy. Make sure that your current viewcontroller was there in navigation stack.

You can check your navigation stack by,

NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);

Put this two line in actionWithTitle method as last line. After presenting imagePicker.

then you will get your view hierarchy.

I assume that you have used navigation controller.

second thing you should dismiss your imagePickerController.

Comments