UISegmentedControl 不起作用

发布于 2024-12-07 19:58:32 字数 479 浏览 1 评论 0原文

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

我的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 命令。这意味着这个方法根本没有被访问?

Hi im trying to use segmented control to swap between three map views however its not working.

My IBAction method is as follows.

- (IBAction)segmentSwitch:(id)sender {

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

    }
    else{
        mapView.mapType = MKMapTypeHybrid; 

    }
}

I have declared UISegementedControl as an outlet and connected it to the xib view. I have also connected this method with touch down/touch up inside/outside. It still doesn't print the NSLog commands given above. Which means this method is not accessed at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉溺在你眼里的海 2024-12-14 19:58:32

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

  1. @interface 中的 IBOutlet UISegmentedControl *segmentControl;(或将其设置为 @property)
  2. - (IBAction) SegmentedControlIndexChanged:(id)sender; 在 .h 中 @end 之前
  3. 将“Segmented Control”拖到视图中并将“Style”更改为 Plain、Bordered 或 “段”的条形
  4. 增量#
  5. 选择“段”并编辑“标题”,确保选中“启用”
  6. 将您的segmentedControl 连接到文件所有者
  7. 连接您的segmentedControlIndexChanged:操作并选择“值已更改”而不是“触摸内部”!
  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 方法。

Quick summary of how to set up a UISegmentedControl in IB for those dealing with more than two segments:

  1. IBOutlet UISegmentedControl *segmentControl; in @interface (or set it as @property)
  2. - (IBAction)segmentedControlIndexChanged:(id)sender; in .h before @end
  3. drag "Segmented Control" into view and change "Style" to Plain, Bordered, or Bar
  4. increment # of "Segments"
  5. Choose "Segment" and edit the "Title" making sure "Enabled" is checked
  6. Connect your segmentedControl to Files Owner
  7. Connect your segmentedControlIndexChanged: action and select "Value Changed" NOT "Touch up Inside"!!
  8. add some code, maybe a switch statement if you have say 4 segments:
-(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;
}
}

In recent iOS versions, you need the braces for each case: or you will get errors. This also shows some bool flagging to keep track of what segment is active, maybe for your willDisplayCell method.

眸中客 2024-12-14 19:58:32

希望您选择了正确的方法 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; 
    }
}

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

希望这对您有帮助。

Hope you have selected the right method ValueChanged and also you have connected the outlet of your method properly.

The only thing you need to do now is to replace your code with your code.

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

Try replacing this code with your code.

Hope this helps you.

半﹌身腐败 2024-12-14 19:58:32

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

selectedSegment 是您的 UISegmentedControl 吗?

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

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

You should use the ValueChanged action for detecting the the switch of segments.

Is selectedSegment your UISegmentedControl?

Then you code should be like:

- (IBAction) segmentSwitch:(id)sender {
    if (self.selectedSegment.selectedSegmentIndex == 0) {
        mapView.mapType = MKMapTypeStandard; 
    } else{
        mapView.mapType = MKMapTypeHybrid; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文