如何使用分段控件更改Button操作

时间:2014-09-10 17:33:33

标签: ios iphone xcode uisegmentedcontrol segment

我有一个按钮,其动作可以推动另一个视图。我的目标是使此按钮根据所选的段打开不同的视图。例如:我有一个名为" Request"的按钮。并有一个控制部分有两个部分,称为"比萨盐"和#34; Sweet Pizza。"例如,当选择" Pizza Salt"在该细分受众群中,然后立即点击"请求"按钮,我想打开一个视图菜单" Salt Pizza。",并选择" Sweet Pizza"我希望按钮能够通过" Sweet Pizza的菜单打开另一个视图。"

注意:我已经准备好了两个带有视图的控制器。

我使用什么代码?

我的代码:

- (IBAction)changeButtonCode:(id)sender {
    if (_firstSegmentSixthView.selectedSegmentIndex == 0);
}

- (IBAction)pushToNextView:(id)sender {


}

2 个答案:

答案 0 :(得分:2)

使用此方法对UIsegmentedControl的不同按钮执行操作:

- (IBAction) segmentControlBtnAction:(id)sender
{
    UISegmentedControl* segmentControl = (UISegmentedControl *)sender;
    int index = [segmentControl selectedSegmentIndex];

    switch(index)
    {
        case 0: // Perform action on first button of segment controller 
                break;
        case 1: // Perform action on second button of segment controller 
                break;
        case 2: // Perform action on second button of segment controller 
                break;
        default 
               break;

    }
}

使用UISegmentControl属性将此方法与xib的{​​{1}}相关联。

答案 1 :(得分:0)

按下请求按钮后:

if (_firstSegmentSixthView.selectedSegmentIndex == 0) //salty
{
    ViewController1 *vc1 = [[ViewController1 alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
else if (_firstSegmentSixthView.selectedSegmentIndex == 1) //sweet
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
}
相关问题