- (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;
}
}
}
编辑:更正了代码
Use the next code (notice that you need the "prev.png" and "next.png" images - arrows):
- (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;
}
}
}
发布评论
评论(2)
使用下一个代码(请注意,您需要“prev.png”和“next.png”图像 - 箭头):
编辑:更正了代码
Use the next code (notice that you need the "prev.png" and "next.png" images - arrows):
EDIT: corrected the code
它可以通过使用具有 2 个段的
UISegmentedControl
来实现。将segmentedControlStyle 设置为
UISegmentedControlStyleBar
。设置 2
UIImage
用于向上和向下查看。It can be implemented by using a
UISegmentedControl
with 2 Segments.Set the segmentedControlStyle as
UISegmentedControlStyleBar
.Set 2
UIImage
for up and down look.