0

I've followed Programmatically creating tab bar for ViewController and How can I create a tab view programmatically on iOS to try and create a tab bar programmatically in my ViewController using objective c, but my tab bar doesn't show up. Here is my code in AppDelegate.m:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];
    self.ref = [[FIRDatabase database] reference];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    ViewController *viewController = [[ViewController alloc]init];
    viewController.title =@"Home";
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:viewController];
    NSArray *viewControllers = [NSArray arrayWithObjects:viewController, nil];
    [viewController release];
    [tabBarController setViewControllers:viewControllers animated:NO];

    // Put the tabBarController's view on the window.
    [self.window addSubview:[tabBarController view]];
    self.window.rootViewController = tabBarController;

    
    return YES;
}

There are no errors, the tab bar just doesn't show up

Willeke
  • 14,578
  • 4
  • 19
  • 47
Beck
  • 5
  • 3
  • Just remove these code: [viewController release] and [self.window addSubview:[tabBarController view]] – childrenOurFuture Jun 21 '21 at 12:34
  • I've tried out your suggestion but it still does not appear. However I put the same code into SceneDelegate.m instead and it appeared, so maybe its due to the newer Xcode version I am using? – Beck Jun 21 '21 at 17:23

1 Answers1

0

I come from Swift, try to learn some Objective-C stuff. I plan to create a Youtube clone app to practice my OC skill.

sceneDelegate is come when Apple published iOS 13. That change apply on both Swift and OC. What is sceneDelegate used for? See difference-between-scenedelegate-and-appdelegate.

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UIWindowScene *windowScene = (UIWindowScene*) scene;
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    UITabBarController *viewController = [[ViewController alloc] init];
    
    _window.rootViewController = viewController;
    [_window makeKeyAndVisible];
    _window.windowScene = windowScene;
}

Instead declare UITabBarController in SceneDelegate, I create a UIViewController subclassed UITbarBarController, because I need this class to do some extra job, like dependency injection.

@interface ViewController ()

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTabBar];
    
}

- (void)configTabBar {
    UIViewController *homeVC = [[HomeViewController alloc] init];
    UIViewController *exploreVC = [[ExploreViewController alloc] init];
    UIViewController *subscriptionVC = [[SubscriptionViewController alloc] init];
    UIViewController *libraryVC = [[LibraryViewController alloc] init];
    
    UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:homeVC];
    UINavigationController *vc2 = [[UINavigationController alloc] initWithRootViewController:exploreVC];
    UINavigationController *vc3 = [[UINavigationController alloc] initWithRootViewController:subscriptionVC];
    UINavigationController *vc4 = [[UINavigationController alloc] initWithRootViewController:libraryVC];
    [self setViewControllers:[NSArray arrayWithObjects:vc1, vc2, vc3, vc4, nil]];
    vc1.title = home;
    vc2.title = explore;
    vc3.title = sub;
    vc4.title = library;
}


@end

Well, it works but I'm not sure if it is the best practice. In 2021, Objective-C learning materials is rare stuff in the internet. I could find 10 Swift tutorials but not 1 OC.

Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32