2015-06-10 172 views
0

我想將登錄場景用作初始視圖控制器並將其連接到選項卡視圖控制器。我不斷收到以下錯誤消息:將初始視圖控制器連接到選項卡視圖控制器

終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [UIViewController中的TabBar]:無法識別的選擇發送到實例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; 
} 
+1

添加您的代碼...我們如何在沒有它的情況下看到問題? – jcoppens

+0

剛添加了初始視圖控制器h文件。我希望我可以上傳故事板的屏幕截圖,以顯示我的意思,但不能,因爲我是一個新的堆棧溢出成員 – shj997

回答

0

我我不是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; 
} 

Hope這有助於。

+0

我上面提供的AppDelegate m文件使用自定義標籤欄項目,所以當我使用您的代碼時,它會擦除所有這些。我不知道是否會更容易刮除標籤欄控制器,併爲每個視圖控制器構建自定義標籤欄,當然除了登錄屏幕。 – shj997

+0

我也使用自定義標籤欄項目。然而,我將它們設置在FirstViewController和SecondViewController中的'(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil'中。我會更新我的代碼以向你展示我的意思。 – ABC

+0

得到它的工作。謝謝! – shj997

相關問題