2011-05-30 87 views
2

你好 我有導航欄 上顯示分段按鈕的問題我試試這個代碼它不工作 我的標籤欄召集這次的TabBar我使用 這代碼的TabBar工作正常,但導航欄上的按鈕沒有調用 按鈕到導航欄段按鈕在iphone顯示在導航欄上

-(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)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

我用於 的同一件事 - (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]; 

} 即使它不工作它給我同樣的錯誤。 在此先感謝 哈里什

+0

我不認爲我得到了你的問題正確。對不起,我不能在這裏進一步幫助。 – 2011-05-30 09:40:04

+0

當我不使用該選項卡時,它顯示segmentBarItem>它意味着當我直接使用導航Push視圖調用其他視圖時認爲它的工作是顯示段項目,但是當調用所有選項卡項目時它不顯示段項目但Tabbar is workinng – Shima 2011-05-30 09:47:53

回答

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]; 
+0

我將它用於 - (id)init方法{all code this [self.navigationBar titleView:segmentedControl]; }它給我錯誤的請求成員'導航欄'在某些不是結構或聯盟 – Shima 2011-05-30 12:11:08

+0

嘗試這種方式, [[self.navigationBar topItem] setTitleView:segmentedControl]; \t \t [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES]; – Anand 2011-05-31 03:04:06

+0

它給出的錯誤不是結構或聯合 – Shima 2011-05-31 04:49:28