Pergunta

I am trying to present a new viewcontroller from a class outside of that viewcontroller.

It looks like this:

AppDelegate.m -> inside has this code:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 

Now, inside of that method is an object which utilises another class: [actionSheet launchActionSheetNav]; this simply makes an actionsheet appear with different options.

Now, inside of ActionSheets.m is some code, involving a segue as follows:

handler:^(AHKActionSheet *as){

    ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
    [vc presentViewController:vc animated:YES completion:nil];
}];

All I want to do, is launch a new view controller from inside of there, however doing [self.navigationController brings an error that ActionSheets.m does not contain such method/property.

How can I present a viewcontroller from outside the original classes?

So the hierarchy is as follows:

Viewing Storyboard > Listening on AppDelegate.m > Inside that, a class method is called which takes you to ActionSheets.m -> and from there I need to display the new viewcontroller.

Foi útil?

Solução

This line:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];

Creates a new instance of ViewControllerYoutube. This ViewController doesn't exist anywhere else but that line, so when you follow it with this:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
[vc presentViewController:vc animated:YES completion:nil];

You're trying to present on a view controller that hasn't yet been presented.

If you want to present from an outside class, you need some way to keep a reference to the view controller you want to present, perhaps in your ActionSheet.h

@property (weak, nonatomic) ViewControllerYoutube *myViewControllerYoutube;

Then assign it when you create your action sheet (assuming you create it in ViewControllerYoutube)

ActionSheet * myActionSheet = [[ActionSheet alloc]init];
myActionSheet.myViewControllerYoutube = self;

Then instead of this:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
[vc presentViewController:vc animated:YES completion:nil];

Call this:

[_myViewControllerYoutube presentViewController:vc animated:YES completion:nil];

UPDATE

Based on our chat, here's how I think we can solve it.

  1. In ActionSheet.h:

    @property (weak, nonatomic) UIViewController *presentingViewController;
    
  2. In 'ActionSheet.m'

    ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
    [_presentingViewController presentViewController:vc animated:YES completion:nil];
    
  3. In your AppDelegate:

    ActionSheet * actionSheet = [[ActionSheet alloc]init];
    UITabBarController * tabController = (UITabBarController *)self.window.rootViewController;
    actionSheet.presentingViewController = tabController.selectedViewController;
    

Outras dicas

you need to do [currentViewController presentViewController:vc animated:YES completion:nil];

do you have any reference to "correntViewController" from this block? from this class? if not, do this

add a property @property(nonatomic,strong) UIViewController *currentViewController

in the given class

then in your init method, or in the usual setCurrentViewController:

pass the reference to the controller, and change you[vc presentViewController:vc animated:YES completion:nil]; to [self.currentViewController presentViewController:vc animated:YES completion:nil];

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top