iPhone按钮未显示在iPhone的导航栏上

时间:2011-05-30 08:55:16

标签: iphone objective-c

您好 我在导航栏上显示分段按钮时遇到问题 我尝试这个代码它不起作用 我有Tab Bar为了调用这个tabBar我使用 此代码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, 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, 0, 50, 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];

} 即使它不起作用也会给我同样的错误。 提前致谢 哈里什

1 个答案:

答案 0 :(得分:0)

将其设为[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, 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];