2009-12-11 74 views
0

我正在使用此代碼獲取導航欄右側的三個按鈕,按鈕是可見的,但在此旁邊,我想從此三個按鈕圖像轉到下一個視圖,文本,視頻。想要從右鍵菜單轉到下一個視圖項目

NSArray *segmentTextContent = [NSArray arrayWithObjects:@"Image",@"Text",@"Video",nil];  
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent]; 

     segmentedControl.frame = CGRectMake(13, 20, 150, kCustomButtonHeight); 
     segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
     segmentedControl.momentary = YES; 

     defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later 

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


    self.navigationItem.rightBarButtonItem = segmentBarItem; 

    if(segmentedControl.selectedSegmentIndex=0) 
    { 

    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; 
    } 
    else if(segmentedControl.selectedSegmentIndex==1) 
    { 

    [segmentedControl addTarget:self action:@selector(segmentAction1:) forControlEvents:UIControlEventValueChanged]; 
    } 
else if(segmentedControl.selectedSegmentIndex==2) 
    { 

    [segmentedControl addTarget:self action:@selector(segmentAction2:) forControlEvents:UIControlEventValueChanged]; 
    }  
      [segmentBarItem release]; 

      //[modalBarButtonItem release];  
} 
return self; 
} 

回答

0

我認爲你對動作和目標的工作原理有一個根本性的誤解。創建時無法檢查selectedSegmentIndex。它將始終爲0.您必須將目標恰好添加到響應方法中一次,並檢查selectedSegmentIndex並從那裏採取行動:

[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvent:UIControlEventTouchUpInside]; 

//... 

- (void) segmentAction:(UISegmentedControl *)source { 
    switch (source.selectedSegmentIndex) { 
    case 0: 
     [self segmentAction0:source]; 
     break; 
    case 1: 
     [self segmentAction1:source]; 
     break; 
    case 2: 
     [self segmentAction2:source]; 
     break; 
    } 
}