Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 更改UISegmentedControl selectedSegmentIndex不';不要更改所选索引_Objective C_Ios - Fatal编程技术网

Objective c 更改UISegmentedControl selectedSegmentIndex不';不要更改所选索引

Objective c 更改UISegmentedControl selectedSegmentIndex不';不要更改所选索引,objective-c,ios,Objective C,Ios,我正在尝试使用UISegmentControl在iOS5的导航栏中创建一个切换按钮。我遇到的问题是setSelectedSegmentIndex不是持久性的,即当用户单击其中一个段时,会调用它的操作,但不会将其设置为selected。我当前的代码如下所示: // In viewDidLoad toolSegment = [[UISegmentedControl alloc] initWithItems: [NSArr

我正在尝试使用UISegmentControl在iOS5的导航栏中创建一个切换按钮。我遇到的问题是setSelectedSegmentIndex不是持久性的,即当用户单击其中一个段时,会调用它的操作,但不会将其设置为selected。我当前的代码如下所示:

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

任何关于我可能做错什么的提示都将不胜感激

您可能不希望控件处于
瞬时
模式,因为这将允许多选。将
瞬时
设置为

将控件设置为瞬时模式的任何特定原因?是否允许用户选择多个内容?如果没有,则将其设置为“否”,所选索引应该可以工作。@Krumelur谢谢,工作正常!很高兴听到这个消息。我的评论只是一个回答,所以你可以接受它并帮助别人。