简体   繁体   中英

Navigation from Appdelegate to tab view

How to navigate from Appdelegate to another view(a tabview). I have tried this

  MyViewController *mvc = [[MyViewController alloc] init];
  UINavigationController *navController = [[[UINavigationController alloc]  initWithRootViewController:mvc] autorelease];
   self.window.rootViewController = navController;

It navigates to MyViewController, but tab bar is hidden,also back button is not working(popView) in that view

I am navigating to a view which is not a tabviewcontroller(only tab bar is hidden)

Try this for Creating Tabbar

tabBar_Controller = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray =[[NSMutableArray alloc]initWithCapacity:2];


firstViewController = [[FirstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
nav.tabBarItem.title = @"item1";
nav.navigationBar.barStyle = UIBarStyleBlack;
[localControllersArray addObject:nav];
[self setNav:nil];

secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
nav = [[UINavigationController alloc]initWithRootViewController:secondViewController];
nav.tabBarItem.title = @"item2";
[localControllersArray addObject:nav];
[self setNav:nil];

tabBar_Controller.viewControllers = localControllersArray;
tabBar_Controller.delegate = self;
tabBar_Controller.selectedIndex = 0;

 self.window.rootViewController = tabBar_Controller;

You can try This also, in your Appdelegate .

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[SPHFirstViewController alloc] initWithNibName:@"SPHFirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[SPHSecondViewController alloc]  initWithNibName:@"SPHSecondViewController" bundle:nil] autorelease];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

    UINavigationController *navigation;

}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

//Main file

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

@synthesize viewController = _viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];

   self.window.rootViewController = navigation;

    [self.window makeKeyAndVisible];

   return YES;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM