分段按钮未显示在iPhone的导航栏中

问题描述

| 你好 我在导航栏上显示分段按钮时遇到问题 我尝试此代码不起作用 我有标签栏用于调用此标签栏 这段代码TabBar正常工作,但导航栏上的Button不显示
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITabBarController *tabcontroller = [[UITabBarController alloc] init];

    UITabBarController *vc1 = [[TMapViewController alloc] init];
    UITabBarController *vc2 = [[TShareController alloc]init];
    UITabBarController *vc3 = [[TSpeedometerController alloc]init];
    UITabBarController *vc4 = [[TReviewsController alloc]init];
    NSArray *viewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,nil];



    [tabcontroller setViewControllers:viewControllers];

    [vc1 release];
    [vc2 release];
    [vc3 release];
    [vc4 release];

    [self.navigationController pushViewController: tabcontroller animated: YES];
}
然后将此代码放入TMapViewController.m中以进行调用 导航栏上的按钮
-(id)init 
{
    UITabBarItem *tbi = [self tabBarItem];
    [tbi setTitle:@\"Map\"];
    UIImage *i = [UIImage imageNamed:@\"mapper_f.png\"];
    [tbi setImage:i];


    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                             [UIImage imageNamed:@\"Activitieslist.png\"],nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0,50,35);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];

    return self;
}
我用到的同样的东西 -(void)viewDidLoad {     [super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                        [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@\"Activitieslist.png\"],nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0,35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
[[self.navigationBar topItem] setTitleView:segmentedControl]; 
[[[self.navigationBar topItem] titleView] setUserInteractionEnabled:YES]; 



TLocationManager *manager = [[TLocationManager alloc] init];
self.locationManager = manager;
self.locationManager.delegate = self;
[manager release];

TMapRoutes *routes = [[TMapRoutes alloc] init];
self.mapRoutes = routes;
self.mapRoutes.mapView = self.myMapView;
[routes release];

self.myMapView.delegate = self;

/* Run the simulator */
[locationManager startUpdatingLocation];
} 即使它不起作用,也会给我同样的错误。 提前致谢 ish     

解决方法

        设置为
[self.navigationBar titleView:segmentedControl]
-(void)loadView { 
 UITabBarItem *tbi = [self tabBarItem];
    [tbi setTitle:@\"Map\"];
    UIImage *i = [UIImage imageNamed:@\"mapper_f.png\"];
    [tbi setImage:i];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                        [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@\"Activitieslist.png\"],nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0,50,35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

[[self.navigationBar topItem] setTitleView:segmentedControl]; [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES];
[segmentBarItem release];
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...