消息发送到解除分配的实例0x9553970?设置UITabbarController的委托

时间:2014-05-31 14:09:33

标签: ios objective-c delegates uitabbarcontroller

在我的iOS应用中,我在MainViewController.m中使用以下方法将一个标签栏控制器替换为rootview控制器中的另一个标签控制器

//Set Tab Bar View as per the Analyst User
-(void)setTabrViewforAnalyst{
    tabBarController_main = [[UITabBarController alloc] init];
    // Create initialized instance of NSMutableArray to hold our UINavigationControllers
    NSMutableArray *tabs = [[NSMutableArray alloc] init];

    tabBarController_main.delegate=self;

    // Create first UIViewController

    PlannedEventViewController *plannedView=[[PlannedEventViewController alloc]initWithNibName:@"PlannedEventViewController" bundle:nil];
    UINavigationController *plannedNavigationView=[[UINavigationController alloc]initWithRootViewController:plannedView];

    // Initialize the UINavigationController

    [tabs addObject:plannedNavigationView];

    MyVideoViewController *videoView=[[MyVideoViewController alloc]initWithNibName:@"MyVideoViewController" bundle:nil];
    UINavigationController *videoNavigationView=[[UINavigationController alloc]initWithRootViewController:videoView];
    // Add UINavigationController to you tabs
    [tabs addObject:videoNavigationView];


    MyTeamViewController *myTeamview=[[MyTeamViewController alloc]initWithNibName:@"MyTeamViewController" bundle:nil];
    UINavigationController *myTeamNavigationView=[[UINavigationController alloc]initWithRootViewController:myTeamview];
     // Add UINavigationController to you tabs
    [tabs addObject:myTeamNavigationView];


    // Create second UIViewController
    SettingViewController *settingView=[[SettingViewController alloc]initWithNibName:@"SettingViewController" bundle:nil];
    UINavigationController *settingNavigationView=[[UINavigationController alloc]initWithRootViewController:settingView];

       settingView.isLoggedIn=YES;
    // Add UINavigationController to you tabs
    [tabs addObject:settingNavigationView];
    // Add the tabs to the UITabBarController



    // Create second UIViewController

    // Add the tabs to the UITabBarController
    [tabBarController_main setViewControllers:tabs];



    tabBarController_main.tabBar.backgroundImage = [UIImage imageNamed:@"tab_back.png"];

    UIImage *selectedImage1 = [UIImage imageNamed:@"my_team.png"];
    UIImage *unselectedImage1 = [UIImage imageNamed:@"my_team.png"];

    UIImage *selectedImage0 = [UIImage imageNamed:@"events.png"];
    UIImage *unselectedImage0 = [UIImage imageNamed:@"events.png"];

    UIImage *selectedImage2 = [UIImage imageNamed:@"video"];
    UIImage *unselectedImage2 = [UIImage imageNamed:@"video.png"];

    UIImage *selectedImage3 = [UIImage imageNamed:@"settings.png"];
    UIImage *unselectedImage3 = [UIImage imageNamed:@"settings.png"];


    UITabBar *tabBar = tabBarController_main.tabBar;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];


    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
    [item1 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
    [item2 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
     [item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];

    item0.title = @"a";
    item1.title = @"b";
    item2.title=@"c";
    item3.title = @"d";

    //Check OS version
    NSString *strOsVersion = [[UIDevice currentDevice] systemVersion];
    float osVersionFloat = [strOsVersion floatValue];

    if (osVersionFloat >= 7.0)
        tabBar.translucent = false;

    tabBar.tintColor = [UIColor colorWithRed:234.0/256.0 green:234.0/256.0 blue:234.0/256.0 alpha:1.0];
     [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_selected_blue2.png"]];

    tabBarController_main.selectedIndex=3;

    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    app.window.rootViewController = tabBarController_main;


}

但是当我在MainViewController.m中将UITabBarControllerDelegate的委托设置为self时,我正在创建标签栏,它会使错误[MainViewController respondsToSelector:]: message sent to deallocated instance 0x9553970的应用程序崩溃,任何人都可以告诉我应该是什么问题? tabBarController_main是我.h文件的属性

1 个答案:

答案 0 :(得分:0)

问题是你要么在预定之前释放你的MainViewController,要么意外地释放了它。您可以在[-MainViewController dealloc]中放置一个断点并在调试器中停止并检查堆栈以查看它发生的原因。