ipalibowhyte
ipalibowhyte

Reputation: 1563

How to make uiviewcontoller display from appdelegate

i am working on an app with uitabbarcontoller(with 5 uiviewcontrollers) and uinavigation bar. I have created all of these in the app but when i run it, it doesn't work

This is the AppDelegate.m class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navCon = [[UINavigationController alloc] init];
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    HomePageView *viewController = [[HomePageView alloc] init];
    FeedViewController *feedViewController=[[FeedViewController alloc]init];
    ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
    PlayViewController *playViewController = [[PlayViewController alloc]init];
    ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
    RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];

    tabBarController.viewControllers=[NSArray arrayWithObjects:feedViewController,profileViewController,playViewController,listeningSessionViewController,recievedViewController, nil];

    //navigating to the UITabBarController that you created
    [navCon pushViewController:tabBarController animated:YES];
    [navCon pushViewController:viewController animated:NO];

    return YES;
}

Upvotes: 1

Views: 79

Answers (2)

Michael
Michael

Reputation: 6899

Instantiate a UITabBarController in Main.storyBoard and connect it to the class tabBarCon in the identity inspector.

Interface:

@interface tabBarCon: UITabBarController

@end

Implementation:

@implementation tabBarCon

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   // Do any additional setup after loading the view.

   HomePageView *viewController = [[HomePageView alloc] init];
   FeedViewController *feedViewController=[[FeedViewController alloc]init];
   ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
   PlayViewController *playViewController = [[PlayViewController alloc]init];
   ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
   RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];


        NSArray *viewControllerArray = [[NSArray alloc]initWithObjects:viewController,
                                                                       feedViewController,
                                                                       profileViewController,
                                                                       playViewController,
                                                                       listeningSessionViewController,
                                                                        recievedViewController,nil];

//Then add buttons
 UITabBarItem *moreItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
 viewController.tabBarItem = moreItem;
//...


 self.viewControllers = viewControllerArray;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 1

In .h

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navC;
@property (strong, nonatomic) UITabBarController *tabC;

in .m in appDidFinish

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

// Initialize your five tab controllers.  with each tab has its own navigation controller
FeedViewController *feedViewController=[[FeedViewController alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:feedViewController];

ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];

PlayViewController *playViewController = [[PlayViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:playViewController];

ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:listeningSessionViewController];

RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];
UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:recievedViewController];

// initialize tabbarcontroller and set your viewcontrollers.
self.tabC = [[UITabBarController alloc]init];
self.tabC.viewControllers=[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5, nil];

// Inititalize Navigationcontroller and set root as tabbar.
self.navC = [[UINavigationController alloc]initWithRootViewController:self.tabC];

// Set Window rootview as navigation.
self.window.rootViewController = self.navC;

// Show window
[self.window makeKeyAndVisible];

Maybe this will help you.

Upvotes: 2

Related Questions