【问题标题】:UISegmentedControl Not functioningUISegmentedControl 不起作用
【发布时间】:2026-02-16 21:10:01
【问题描述】:

您好,我正在尝试使用分段控制在三个地图视图之间进行交换,但它不起作用。

我的IBAction方法如下。

- (IBAction)segmentSwitch:(id)sender {

    NSLog(@"inside segmented switch");
    NSLog(@"selected segment %@",selectedSegment);
    if (selectedSegment == 0) {
        mapView.mapType = MKMapTypeStandard; 

    }
    else{
        mapView.mapType = MKMapTypeHybrid; 

    }
}

我已将 UISegementedControl 声明为插座并将其连接到 xib 视图。我还将这种方法与内/外触地/触地连接起来。它仍然不打印上面给出的 NSLog 命令。这意味着根本无法访问此方法?

【问题讨论】:

  • 是的,但现在它甚至没有进入 segmentedSwitch 方法。意思是我的 NSLog 命令没有打印,这意味着它甚至不在那里?
  • 使用更改的值,而不是在内部修饰或任何其他事件类型

标签: ios iphone objective-c uisegmentedcontrol


【解决方案1】:

快速总结如何在 IB 中为处理两个以上段的用户设置 UISegmentedControl:

  1. @interface 中的IBOutlet UISegmentedControl *segmentControl;(或设置为@property)
  2. - (IBAction)segmentedControlIndexChanged:(id)sender; 在@end 之前的.h 中
  3. 将“Segmented Control”拖到视图中并将“Style”更改为Plain、Bordered或Bar
  4. 增加“段”的数量
  5. 选择“Segment”并编辑“Title”,确保选中“Enabled”
  6. 将您的 segmentedControl 连接到文件所有者
  7. 连接您的 segmentedControlIndexChanged: 操作并选择“Value Changed”而不是“Touch up Inside”!!
  8. 添加一些代码,如果您有 4 个段,可以添加一个 switch 语句:

-(IBAction)segmentedControlIndexChanged:(id)sender {

NSLog(@"segmentedControlIndexChanged");

switch (segmentControl.selectedSegmentIndex)
{
    case 0:
    {
        NSLog(@"dateSegmentActive");
        dateSegmentActive = YES;
        noteSegmentActive = NO;
        typeSegmentActive = NO;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 1:
    {
        NSLog(@"noteSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = YES;
        typeSegmentActive = NO;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 2:
    {
        NSLog(@"typeSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = NO;
        typeSegmentActive = YES;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 3:
    {
        NSLog(@"userIDSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = NO;
        typeSegmentActive = NO;
        userIDSegmentActive = YES;
        [yourTable reloadData];
    }
        break;

    default:
        break;
}
}

在最近的 iOS 版本中,每种情况都需要大括号:否则会出错。这也显示了一些布尔标记来跟踪哪个段是活动的,可能是你的 willDisplayCell 方法。

【讨论】:

  • 不。我在想TouchUpInside 不是使用分段控件注册的事件。我试过.AllEvents,但效果不佳。我的预期用途是控件内的任何触摸,即使值没有改变(触摸相同的选定值)。你有这方面的经验吗?
【解决方案2】:

希望您选择了正确的方法ValueChanged,并且您已正确连接方法的插座。

您现在唯一需要做的就是用您的代码替换您的代码。

- (IBAction)segmentSwitch:(UISegmentedControl *)sender 
{
    NSLog(@"inside segmented switch");
    NSLog(@"selected segment %d",sender.selectedSegmentIndex);
    if (sender.selectedSegmentIndex == 0) 
    {
        mapView.mapType = MKMapTypeStandard; 

    }
    else
    {
        mapView.mapType = MKMapTypeHybrid; 
    }
}

尝试用您的代码替换此代码。

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    您应该使用 ValueChanged 操作来检测段的切换。

    selectedSegment是你的UISegmentedControl吗?

    那么你的代码应该是这样的:

    - (IBAction) segmentSwitch:(id)sender {
        if (self.selectedSegment.selectedSegmentIndex == 0) {
            mapView.mapType = MKMapTypeStandard; 
        } else{
            mapView.mapType = MKMapTypeHybrid; 
        }
    }
    

    【讨论】:

    • 是的,但现在它甚至没有进入 segmentedSwitch 方法。意思是我的 NSLog 命令没有打印,这意味着它甚至不在那里?