ios 为 UISegmentedControl 定义点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3049169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define click event for UISegmentedControl
提问by Warrior
I have added a UISegmentedControl
in my application. None of the buttons are selected in normal state. I want to implement a button click event when the first segment is selected, and another event when another button is clicked.
我UISegmentedControl
在我的应用程序中添加了一个。在正常状态下没有选择任何按钮。我想在选择第一个片段时实现一个按钮单击事件,并在单击另一个按钮时实现另一个事件。
回答by macbirdie
If I understand your question correctly, you simply have to implement a target-action method (supported by UIControl
which is UISegmentedControl
's parent class) for the constant UIControlEventValueChanged
, exactly like in the example given in UISegmentControl's reference documentation.
如果我没有理解你的问题,你只需要实现一个目标-动作方法(支持UIControl
它UISegmentedControl
的父类)为恒UIControlEventValueChanged
,酷似UISegmentControl在给出的例子参考文档。
i.e.
IE
[segmentedControl addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
used for a message with the following signature:
用于具有以下签名的消息:
- (void)action:(id)sender
or
或者
[segmentedControl addTarget:self
action:@selector(action:forEvent:)
forControlEvents:UIControlEventValueChanged];
for
为了
- (void)action:(id)sender forEvent:(UIEvent *)event
or
或者
[segmentedControl addTarget:self
action:@selector(action)
forControlEvents:UIControlEventValueChanged];
for the simplest method:
对于最简单的方法:
- (void)action
which are standard types of target-action selectors used in UIKit.
这是 UIKit 中使用的标准类型的目标操作选择器。
回答by Nuno Ferro
try this:
尝试这个:
- (IBAction)segmentSwitch:(UISegmentedControl *)sender {
NSInteger selectedSegment = sender.selectedSegmentIndex;
if (selectedSegment == 0) {
}
else{
}
}
回答by Sivanathan
Try this one,
试试这个,
UISegmentedControl * travelModeSegment = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:NSLocalizedString(@"Driving", nil), NSLocalizedString(@"Walking", nil), nil]];
[travelModeSegment setFrame:CGRectMake(9.0f, 0.0f, 302.0f, 45.0f)];
[cell addSubview:travelModeSegment];
[travelModeSegment release];
then write an action,
然后写一个动作,
if (travelModeSegment.selectedSegmentIndex == 0) {
//write here your action when first item selected
} else {
//write here your action when second item selected
}
I hope it will help you
我希望它会帮助你
回答by Beninho85
Simple version in swift updated
快速更新的简单版本
func loadControl(){
self.yourSegmentedControl.addTarget(self, action: #selector(segmentSelected(sender:)), forControlEvents: .valueChanged)
}
func segmentSelected(sender: UISegmentedControl)
{
let index = sender.selectedSegmentIndex
// Do what you want
}
回答by Jonathan
It's worth noting that the selector has to match a method exactly, but without the actual parameters.
值得注意的是,选择器必须与方法完全匹配,但没有实际参数。
@selector(action)
=> -(void) action { ..code.. }
@selector(action:)
=> -(void) action:(id)sender { ..code.. }
@selector(action:forEvent:)
=> -(void) action:(id)sender forEvent:(UIEvent *)event { ..code.. }
This confused me for the longest time, and it's not quite clear from the earlier answers.
这让我困惑了最长的时间,而且从早期的答案中还不清楚。
回答by datayeah
for SWIFT use this:
对于 SWIFT 使用这个:
segmentedControl.addTarget( self, action: "segmentSelected:", forControlEvents: UIControlEvents.ValueChanged )
and implement a method like this:
并实现这样的方法:
func segmentSelected(sender: UISegmentedControl)
{
println("selected index: \(sender.selectedSegmentIndex)")
}
Extend UISegmentedControl if you want to call a specific action when the selected segment is selected:
如果您想在选定片段被选中时调用特定操作,请扩展 UISegmentedControl:
class CustomSegmentedControl : UISegmentedControl
{
override init()
{
super.init()
}
override init(frame:CGRect)
{
super.init(frame:frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func valueChanged(index:Int)
{
// ACTION GOES HERE
}
}
and call your "valueChanged" in "segmentSelected" like this:
并在“segmentSelected”中调用您的“valueChanged”,如下所示:
func segmentSelected(sender: CustomSegmentedControl)
{
println("selected index: \(sender.selectedSegmentIndex)")
sender.valueChanged(sender.selectedSegmentIndex)
}
回答by rafaeljuzo
Simple like that, you need to make the action using the storyboard to the view controller in order to make this action to work.
就这么简单,您需要使用情节提要对视图控制器进行操作,以使此操作起作用。
- (IBAction)searchSegmentedControl:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex) {
case 0:
NSLog(@"First was selected");
break;
case 1:
NSLog(@"Second was selected");
break;
case 2:
NSLog(@"Third was selected");
break;
default:
break;
}
}
回答by progrmr
You can attach a handler in IB to the value changed event: UIControlEventValueChanged
您可以将 IB 中的处理程序附加到值更改事件: UIControlEventValueChanged
or you can do it in code:
或者你可以在代码中做到这一点:
[segmentedControl addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];