【问题标题】:Objective c: Tab bar not showing up目标c:标签栏不显示
【发布时间】:2021-09-04 20:54:20
【问题描述】:

我跟随Programmatically creating tab bar for ViewControllerHow can I create a tab view programmatically on iOS 尝试使用objective c 在我的ViewController 中以编程方式创建一个标签栏,但是我的标签栏没有出现。这是我在 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;
}

没有错误,只是标签栏不显示

【问题讨论】:

  • 只需删除这些代码:[viewController release] 和 [self.window addSubview:[tabBarController view]]
  • 我已经尝试了您的建议,但仍然没有出现。但是我将相同的代码放入 SceneDelegate.m 并出现了,所以可能是由于我使用的是较新的 Xcode 版本?

标签: objective-c uitabbarcontroller


【解决方案1】:

我来自 Swift,尝试学习一些 Objective-C 的东西。我打算创建一个 Youtube 克隆应用来练习我的 OC 技能。

sceneDelegate 在 Apple 发布 iOS 13 时出现。该更改同时适用于 SwiftOCsceneDelegate 是做什么用的?见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;
}

而不是在 SceneDelegate 中声明 UITabBarController,我创建了一个 UIViewController 的子类 UITbarBarController,因为我需要这个类来做一些额外的工作,比如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

嗯,它有效,但我不确定这是否是最佳做法。 2021年,Objective-C学习资料在互联网上是难得一见的东西。我可以找到 10 个 Swift 教程,但找不到 1 个 OC。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 2020-08-31
    • 2014-10-10
    • 2018-11-16
    • 2014-04-14
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多