为什么我调用此方法然后当前VC自动返回rootVC

时间:2016-07-16 03:25:03

标签: ios objective-c

我称这种方法为

[self dismissViewControllerAnimated:true completion: nil];

然后当前VC自动返回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) }]; }

我不知道我在哪里设置错误,但是在另一个项目中的代码没有错,请帮我解决这个问题,谢谢!

  

最新更新:

提出错误的地方:

我在ViewWillAppear

中放置了UITabBarController初始化
  代码前

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

  

代码之后:

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

  虽然问题已经解决了,但是我还是不确定为什么UITabBarController在viewWillAppear上初始化(presentViewController然后dismissViewController。当前的VC被释放,并自动回到rootVC)?

3 个答案:

答案 0 :(得分:2)

dismissViewControllerAnimated会弹回您导航堆栈中可用的上一个视图控制器。因此,请确保您没有从导航层次结构中释放以前的viewcontrollers。确保当前的viewcontroller存在于导航堆栈中。

您可以按

检查导航堆栈
NSArray *arr = [self.navigationController viewControllers];
NSLog(@"%@",arr);

将这两行放在actionWithTitle方法中作为最后一行。在提交imagePicker

之后

然后您将获得视图层次结构。

我假设您使用过导航控制器。

第二件事你应该解雇你的imagePickerController

答案 1 :(得分:1)

您在自己上展示UIImagePickerController,然后尝试在self方法中解除imagePickerController: didFinishPickingMediaWithInfo:

self不是你想要解雇的。

请尝试解雇UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.user_icon.image = [info objectForKey:UIImagePickerControllerEditedImage];

    // CHANGE BELOW FROM SELF TO PICKER
    [picker dismissViewControllerAnimated:true completion: nil];
}

答案 2 :(得分:1)

您的imagePicker没有任何问题。图像选择器正在运行。 我可能已经找到了原因。

viewWillAppear()

如果出现视图,此行将加载制表符,将所有导航设置为其根视图控制器。

所以,在你的情况下,当imagePicker被解雇时。

[self loadTabBarControllers];

将执行并再次设置标签栏。

从viewWillAppear方法中删除on adding folder items to this_folder after receiving added_items set added_Items_List to {} repeat with oneItem in added_items set fileName to name of (info for oneItem) if fileName does not start with "." then set end of added_Items_List to fileName end if end repeat if (count added_Items_List) = 0 then return ....