如何响应用户按下 UISegment?

发布于 2024-09-29 15:03:59 字数 69 浏览 0 评论 0原文

如何响应用户按下 UISegment?是否有委托,或者我必须以编程方式(或 Interface Builder)附加选择器?

How to I respond to a user pressing a UISegment? Is there a delegate, or must I programmatically (or Interface Builder), attach the selectors?

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

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

发布评论

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

评论(2

因为看清所以看轻 2024-10-06 15:03:59

如果您更喜欢在代码中执行此操作,您可以使用:

[segmentedControl addTarget:self action:@selector(didSelectIndex:) forControlEvents:UIControlEventValueChanged];

这就是将被调用的方法

- (void) didSelectIndex: (id) sender
{
    NSLog(@"%@",[(UISegmentedControl *)sender titleForSegmentAtIndex:[(UISegmentedControl *)sender selectedSegmentIndex]]); //replace this with your code
}

。如果您更喜欢使用 IB,请右键单击您的 UISegmentedControl,选择 Value Changed,然后将其附加到您的第一响应者中所需的方法。

If you prefer to do it in code you can use:

[segmentedControl addTarget:self action:@selector(didSelectIndex:) forControlEvents:UIControlEventValueChanged];

And this is then the method that would be called

- (void) didSelectIndex: (id) sender
{
    NSLog(@"%@",[(UISegmentedControl *)sender titleForSegmentAtIndex:[(UISegmentedControl *)sender selectedSegmentIndex]]); //replace this with your code
}

If you prefer to use IB, right click on your UISegmentedControl select Value Changedand then attach it to the desired method in your first responder.

眼泪都笑了 2024-10-06 15:03:59

UISegmentedControl 是 UIControl 的子类,只要选定的段发生更改,就会发出控件事件 UIControlEventValueChanged。您可以在代码中为此事件添加目标/操作对,也可以使用 IB 中的常规 Control 单击并拖动来完成此操作。

UISegmentedControl subclasses UIControl and sends out the control event UIControlEventValueChanged whenever the selected segment changes. You can add a target/action pair for this event in code, or you can do it with the normal control-click-and-drag in IB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文