How to launch a ViewController from a Non ViewController class?

后端 未结 7 1912
星月不相逢
星月不相逢 2020-12-22 09:48

the title says it all, i usually open ViewControllers like this

ListingViewController *listingView = [[ListingViewController alloc]init];
[self.navigationCon         


        
相关标签:
7条回答
  • 2020-12-22 10:12

    self.navigationController can be accessed via only UIViewController's subclass. You want to access UINavigationController from not UIViewcontroller class. Is it what you are asking?

    If you have property of UINavigationController at AppDelegate like

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) UINavigationController *navigationController;
    @end
    

    You can get a pointer from wherever you want as below

    #import "AppDelegate.h"
    AppDelegate *delegate = (AppDelegate *)[[[UIApplication sharedApplication] delegate];
    UINavigationController *navigation = [delegate navigationController];
    

    Now you have it.

    0 讨论(0)
  • 2020-12-22 10:14

    You are looking to add detail views to a cell try this link on Adding Detail Views.Hopefully this Helps

    0 讨论(0)
  • 2020-12-22 10:17

    You have to notify the ViewController, on cell click, on which UICollectionView exist. and on this controller you can push the desired controller

    0 讨论(0)
  • 2020-12-22 10:23

    You need a view controller of some sort for pushing or presenting, there's no way around that.

    You shouldn't have a problem accomplishing that and there are a few ways you can do so. First off, you should be able to write this logic in the UICollectionViewDelegate method collectionView:didSelectItemAtIndexPath:. Your delegate is most likely the containing view controller so that may be all you need to do. If however it isn't, just add a view controller property to your delegate that you can use for presenting things. Make it a weak one to avoid a possible retain cycle (where the view controller retains the delegate and the delegate retains the view controller).

    0 讨论(0)
  • 2020-12-22 10:32

    Use the UICollectionViewDelegate method didSelectItemAtIndexPath for doing this and make sure that this delegate method is present in the view controller class implementing the UICollectionView.

    0 讨论(0)
  • 2020-12-22 10:32

    You need to get the current UIViewController, there are multiple ways to do it. You can parse the VC in your UICollectionViewCell's header file. You can also use self.window.rootviewcontroller.

    After that you can just use [VC pushViewController:listingView animated:YES]; or [self.window.rootviewcontroller pushViewController:listingView animated:YES];

    0 讨论(0)
提交回复
热议问题