将初始视图控制器连接到选项卡视图控制器

Connect initial view controller to tab view controller

我想使用登录场景作为初始视图控制器并将其连接到选项卡视图控制器。我不断收到以下错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tabBar]: unrecognized selector sent to instance 0x7fe85a560fd0'

//  LogInViewController.h
#import <UIKit/UIKit.h>

@interface LogInViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txtUsername;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;

- (IBAction)sigininClicked:(id)sender;

- (IBAction)backgroundTap:(id)sender;
@end


//  AppDelegate.m
// 

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

// Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

tabBarItem1.title = @"Trucks";
tabBarItem2.title = @"Dashboard";
tabBarItem3.title = @"Map";
tabBarItem4.title = @"Settings";

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"iu.png"]
          withFinishedUnselectedImage:[UIImage imageNamed:@"iu.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"dashboard.png"]
          withFinishedUnselectedImage:[UIImage imageNamed:@"dashboard.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"globe.png"]
          withFinishedUnselectedImage:[UIImage imageNamed:@"globe.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings.png"]
          withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];

return YES;
}

我不是 ios 方面的专家,但这是我最近在我的应用程序中以编程方式(没有情节提要)执行此操作的方法。这是从我的 appdelegate 中提取的代码,但它也应该适用于您的目的。

.h 文件中:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
      @property (strong, nonatomic) UITabBarController *tabBarController;
      @property (strong, nonatomic) UIWindow *window;

.m 文件中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.tabBarController = [[UITabBarController alloc] init];
    FirstViewController *firstvc = [[FirstViewController alloc] init];
    SecondViewController *secondvc = [[SecondViewController alloc] init];
    tabBarController.viewControllers = @[firstvc, secondvc];
    self.window.rootViewController = tabBarController;
}

编辑:

在我的 FirstViewController 和 SecondViewController .m 文件中:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self){
         self.tabBarItem.image = [UIImage imageNamed:@"img_vc1"];
         self.tabBarItem.title = @"My 1st View";
    }
    return self;
}

希望这对您有所帮助。