2010-06-30 70 views

回答

7

使用下面的代碼(注意,你需要 「prev.png」 和 「next.png」 圖像 - 箭頭):

- (void)addNextPrevSegmentedControl { 
    // Prepare an array of segmented control items as images 
    NSArray *nextPrevItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"prev.png"], [UIImage imageNamed:@"next.png"], nil]; 
    // Create the segmented control with the array from above 
    UISegmentedControl* nextPrevSegmentedControl = [[UISegmentedControl alloc] initWithItems:nextPrevItems]; 
    [nextPrevSegmentedControl addTarget:self action:@selector(nextPrevAction:) forControlEvents:UIControlEventValueChanged]; 
    // Create the bar button item with the segmented control from above 
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:nextPrevSegmentedControl]; 
    // Add the bar button item from above to the navigation item 
    [self.navigationItem setRightBarButtonItem:rightButton animated:YES]; 
    // Release memory 
    [rightButton release]; 
    [nextPrevSegmentedControl release]; 
} 
- (void)nextPrevAction:(id)sender { 
    if ([sender isKindOfClass:[UISegmentedControl class]]) { 
     int action = [(UISegmentedControl *)sender selectedSegmentIndex]; 

     switch (action) { 
      case 0: 
       // Prev 
       break; 
      case 1: 
       // Next 
       break; 
     } 
    } 
} 


編輯:糾正代碼

+0

謝謝代碼 – Ameya 2010-06-30 08:58:24

+1

'[nextPrevSegmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];''可能需要,因爲我們正在談論NavigationBar項目。 – 2011-09-27 21:20:44

+1

'[nextPrevSegmentedControl setMomentary:YES];'允許多次按下按鈕。 – 2011-09-28 10:10:36

1

它可以通過使用具有2段的UISegmentedControl來實現。

將segmentedControlStyle設置爲UISegmentedControlStyleBar

設置2 UIImage上下看。

+0

三江源代碼 – Ameya 2010-06-30 09:00:10

相關問題