2010-06-30 18 views

Trả lời

7

Sử dụng mã tiếp theo (lưu ý rằng bạn cần "prev.png" và hình ảnh "next.png" - mũi tên):

- (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; 
     } 
    } 
} 


EDIT: chỉnh mã

+0

Thankyou cho mã – Ameya

+1

'[nextPrevSegmentedControl setSegmentedControlStyle: UISegmentedControlStyleBar];' có thể cần thiết, vì chúng ta đang nói về các mục NavigationBar. –

+1

'[nextPrevSegmentedControl setMomentary: YES];' để cho phép nhiều lần nhấn các nút. –

1

Có thể triển khai bằng cách sử dụng UISegmentedControl với 2 Phân đoạn.

Đặt phân đoạnControlStyle là UISegmentedControlStyleBar.

Đặt 2 UIImage để xem lên và xuống.

+0

Thankyou cho mã số – Ameya

Các vấn đề liên quan