我正在尝试使用UISegmentControl在iOS5的导航栏中创建一个切换按钮。我遇到的问题是setSelectedSegmentIndex不是持久的,即当用户点击其中一个段时,它的动作被调用但它没有被设置为选中。我目前的代码如下:
// In viewDidLoad
toolSegment = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"ToolPenIcon.png"],
[UIImage imageNamed:@"TextIcon.png"],
nil]];
[toolSegment addTarget:self action:@selector(changeMode:) forControlEvents:UIControlEventValueChanged];
toolSegment.frame = CGRectMake(0, 0, 85, 30);
toolSegment.segmentedControlStyle = UISegmentedControlStyleBezeled;
toolSegment.momentary = YES;
[toolSegment setSelectedSegmentIndex:1];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:toolSegment];
self.navigationItem.rightBarButtonItem = segmentBarItem;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- (void)changeMode:(id)sender
{
UISegmentedControl * control = (UISegmentedControl*)sender;
if (control.selectedSegmentIndex == 0) {
NSLog(@"Print");
[toolSegment setSelectedSegmentIndex:0];
} else {
// Should change the selected index.
[toolSegment setSelectedSegmentIndex:1];
[pageView changeToText];
if (pageView.canvas.image != nil) {
[self createNewPage];
}
writing = YES;
}
}
任何有关我可能做错的提示都将不胜感激。
答案 0 :(得分:2)
您可能不希望控件处于momentary
模式,因为这样可以进行多选。将momentary
设为NO
。