ios 如何从单独的 IBAction 检测 UISegmentedControl 中的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14100512/
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
How to detect change in UISegmentedControl from a separate IBAction
提问by mstace
I have a UISegmentedControl
button with three segments.
In ViewController.m
this is working just fine -- pressing the buttons fires the correct methods.
我有一个UISegmentedControl
带三段的按钮。在ViewController.m
这工作得很好 - 按下按钮会触发正确的方法。
I have another separate UIButton
that when it is pressed it needs to first CHECK the state of the UISegmentedControl
(to see which button is currently pressed) and then fire a method according to that segment value.
我有另一个单独的UIButton
,当它被按下时,它需要首先检查UISegmentedControl
(查看当前按下哪个按钮)的状态,然后根据该段值触发一个方法。
Here is my code for that separate UIButton
. The button itself is working, but I cannot seem to figure out how to GET the current value of the segment of the UISegmentedControl
.
这是我的单独UIButton
. 按钮本身正在工作,但我似乎无法弄清楚如何获取UISegmentedControl
.
Many thanks for any assistance here.
I am new to OBJ-C
. I know how to do this in VisualBasic
, so answers that are on the more verbose side would be most appreciated as I need to know the 'why'. Thank you.
非常感谢这里的任何帮助。我是新手OBJ-C
。我知道如何在VisualBasic
. 谢谢你。
- (IBAction)decodeButton:(id)sender {
UISegmentedControl *segment = [UISegmentedControl alloc]; // THIS DOES NOT WORK.
if (segment.selectedSegmentIndex == 0) {
decode(textToDecode);
} else if(segment.selectedSegmentIndex == 1) {
decode1(textToDecode);
} else if(segment.selectedSegmentIndex == 2) {
decode2(textToDecode);
}
}
回答by Siba Prasad Hota
Hereis a Tutorial using UISegmentedControl in iOS.
这是在 iOS 中使用 UISegmentedControl的教程。
Just Create a Reference object and wire it properly to File Owner.
只需创建一个参考对象并将其正确连接到文件所有者。
IBOutlet UISegmentedControl *segmentedControl;
Then set property
然后设置属性
@property (strong, nonatomic) IBOutlet UISegmentedControl * segmentedControl;
Synthesize in .m file
在 .m 文件中合成
@synthesize segmentedControl;
Now You can Access the selected index at any time.
现在您可以随时访问所选索引。
- (IBAction)decodeButton:(id)sender {
if (segmentedControl.selectedSegmentIndex == 0) {
decode(textToDecode);
} else if(segmentedControl.selectedSegmentIndex == 1) {
decode1(textToDecode);
} else if(segmentedControl.selectedSegmentIndex == 2) {
decode2(textToDecode);
}
}
回答by Senthilkumar
Your code alloc
every time UISegmentedControl
in the button press action. So use the following code for sUISegmentedControl
creation and its action .
您的代码alloc
每次都UISegmentedControl
在按钮按下动作中。因此,使用以下代码进行sUISegmentedControl
创建及其操作。
SegmentChangeView=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Segment1",@"Segment2",@"Segment3",nil]];
SegmentChangeView.frame=CGRectMake(5, 44, self.view.bounds.size.width-10, 33);
SegmentChangeView.selectedSegmentIndex=0;
SegmentChangeView.segmentedControlStyle=UISegmentedControlStyleBar;
SegmentChangeView.momentary = YES;
[SegmentChangeView setTintColor:[UIColor blackColor]];
NSDictionary *attributes =[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13],UITextAttributeFont,nil];
[SegmentChangeView setTitleTextAttributes:attributes forState:UIControlStateNormal];
[SegmentChangeView addTarget:self action:@selector(SegmentChangeViewValueChanged:) forControlEvents:UIControlEventValueChanged];
SegmentChangeView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:SegmentChangeView];
-(IBAction)SegmentChangeViewValueChanged:(UISegmentedControl *)SControl
{
if (SControl.selectedSegmentIndex==0)
{
decode(textToDecode);
}
else if (SControl.selectedSegmentIndex==1)
{
decode1(textToDecode);
}
else if (SControl.selectedSegmentIndex==2)
{
decode2(textToDecode);
}
}
回答by D_D
You should remove UISegmentedControl *segment = [UISegmentedControl alloc] ;
from your code, as it allocs anew instance of your UISegmentedControl every time, instead,
您应该UISegmentedControl *segment = [UISegmentedControl alloc] ;
从代码中删除,因为它每次都会分配一个新的 UISegmentedControl 实例,
create an outlet for you UISegmentController
like
为您创造一个出口UISegmentController
像
@property (strong, nonatomic) IBOutlet UISegmentedControl * segment;
and then later at any point in your viewcontroller.m
file, you can get the currently selected segment by using
然后在viewcontroller.m
文件中的任何一点,您都可以使用
segment.selectedSegmentIndex;
Hope this make sense,
希望这是有道理的,
Regards
问候
回答by Rajesh Loganathan
Try like this
像这样尝试
- (IBAction)segmentedControlChanged:(id)sender
{
UISegmentedControl *s = (UISegmentedControl *)sender;
if (s.selectedSegmentIndex == 1)
{
//code
}
else
{
//code
}
}
回答by junaidsidhu
This code means you are creating a new Object on every click
此代码意味着您在每次单击时都会创建一个新对象
UISegmentedControl *segment = [UISegmentedControl alloc] ;
The thing you have to do take IBOutlet
(Property) of your segmentedControl
then I will work for you. dont create a new object in the button method. when you will make a IBOutlet it will be link with at segmentControl
and your code will work that time . Thanks
你必须做的事情拿走你的IBOutlet
(财产)segmentedControl
然后我会为你工作。不要在按钮方法中创建新对象。当您制作 IBOutlet 时,它将与 at 链接,segmentControl
届时您的代码将起作用。谢谢