从AppDelegate启动ViewController

时间:2014-09-17 13:07:04

标签: ios uiviewcontroller appdelegate

我有自定义网址方案,我想打开某个ViewController,当我转到此网址时,该ViewController不是根。我已经能够做到这一点,剩下的就是将navigationController推送到我AppDelegate的{​​{1}},我可以像这样处理我的网址:

- (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。 知道我怎么能以不同的方式做到这一点?

6 个答案:

答案 0 :(得分:11)

问题

要处理从 AppDelegate 推送和弹出viewControllers,您需要使用[UIApplication sharedApplication]跟踪所有viewControllers,从root 1开始。


解决方案

推送 ViewController来自 AppDelegate

ListingViewController *listingVC = [[ListingViewController alloc] init];
[(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES];

POP 您刚才提供的ViewController,您需要使用此代码

[(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];

答案 1 :(得分:7)

如果您使用故事板,那么您可以使用以下行来推送您的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];

答案 2 :(得分:2)

在didFinishingWithLaunchingOptions

中编写此代码
  SecViewController *Vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"second"];
   [(UINavigationController *)self.window.rootViewController pushViewController:Vc animated:YES];

答案 3 :(得分:1)

对于Swift 3版本:

el.sendKeys(Keys.ENTER);

答案 4 :(得分:0)

如果你想推送一个UIViewController,你可能忘记用NibName初始化它:

LoginViewController *loginViewController = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];

答案 5 :(得分:0)

您的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;
}