[英]Launching ViewController from AppDelegate
我有一个自定义URL方案,我想打开一个特定的ViewController
,当我转到这个URL时,它不是根。 我已经能够做到这一点,剩下的就是从AppDelegate
将这个ViewController
推送到navigationController
,我在这里处理我的URL:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([[url scheme] isEqualToString:@"njoftime"]) {
NSDictionary *getListingResponse = [[NSDictionary alloc]init];
getListingResponse = [Utils getListing:[url query]];;
if ([[getListingResponse objectForKey:@"status"] isEqualToString:@"success"]) {
ListingViewController *listingView = [[ListingViewController alloc]init];
[self.window.rootViewController.navigationController pushViewController:listingView animated:YES];
return YES;
}
但它只启动我的应用程序,而不是我想要启动的ListingViewController
。 知道我怎么能以不同的方式做到这一点?
要处理从AppDelegate推送和弹出viewControllers,您需要使用[UIApplication sharedApplication]
跟踪所有viewControllers,从root 1开始。
从AppDelegate 推送 ViewController
ListingViewController *listingVC = [[ListingViewController alloc] init];
[(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES];
弹出您刚才介绍的是视图控制器,你需要使用这个代码
[(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];
如果您使用故事板,那么您可以使用以下行来推送您的VC。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YOURCLASS *obj=[storyboard instantiateViewControllerWithIdentifier:@"YOUR_CLASS_STORYBOARD_ID"];
[self.window.rootViewController.navigationController pushViewController:obj animated:YES];
更新: -
ListingViewController *listingVC = [[ListingViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:listingVC];
[self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
在didFinishingWithLaunchingOptions中编写此代码
SecViewController *Vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"second"];
[(UINavigationController *)self.window.rootViewController pushViewController:Vc animated:YES];
对于Swift 3版本:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "initial") as! UINavigationController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
如果你想推送一个UIViewController,你可能忘记用NibName初始化它:
LoginViewController *loginViewController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
您的AppDelegate.m应如下所示:
#import "TestViewController.h"
@interface AppDelegate ()
@property (strong, nonatomic) TestViewController *viewController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[TestViewController alloc]init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.