分段按钮未显示在 iPhone 的导航栏上
Posted
技术标签:
【中文标题】分段按钮未显示在 iPhone 的导航栏上【英文标题】:Segment Button is not displaying on the navigation bar in iphone 【发布时间】:2011-05-30 08:55:15 【问题描述】:你好 我在导航栏上显示分段按钮时遇到问题 我试试这段代码它不起作用 我有标签栏为了调用这个标签栏,我使用 此代码 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 [超级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];
即使它不起作用,它也会给我同样的错误。 提前致谢 哈里什
【问题讨论】:
我认为我没有正确解决您的问题。抱歉,我不能在这里提供更多帮助。 当我不使用选项卡时,它会显示 segmentBarItem >这意味着当我使用导航直接调用另一个视图时,推送视图认为它的工作是显示段项,但是当调用所有选项卡项时它不显示段项,但 Tabbar 正在工作 【参考方案1】:设置为[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];
【讨论】:
我在 -(id)init 方法中使用了这个所有代码都是这个 [self.navigationBar titleView:segmentedControl]; 它给了我对成员“navigationBar”的错误请求,而不是结构或联合 试试这个方法,[[self.navigationBar topItem] setTitleView:segmentedControl]; [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES]; 不要写在 init 方法中。在 loadview 方法中编写整个代码。 @Anand 看上面我做出改变 我想说的是。在 init 方法中不会创建导航控制器,它将在 loadview 中创建,并且您正在尝试为其设置对象。因此你得到了错误。以上是关于分段按钮未显示在 iPhone 的导航栏上的主要内容,如果未能解决你的问题,请参考以下文章