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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:41:02  来源:igfitidea点击:

How to detect change in UISegmentedControl from a separate IBAction

objective-ciosuisegmentedcontrol

提问by mstace

I have a UISegmentedControlbutton with three segments. In ViewController.mthis is working just fine -- pressing the buttons fires the correct methods.

我有一个UISegmentedControl带三段的按钮。在ViewController.m这工作得很好 - 按下按钮会触发正确的方法。

I have another separate UIButtonthat 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 allocevery time UISegmentedControlin the button press action. So use the following code for sUISegmentedControlcreation 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 UISegmentControllerlike

为您创造一个出口UISegmentController

@property (strong, nonatomic) IBOutlet UISegmentedControl * segment;

and then later at any point in your viewcontroller.mfile, 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 segmentedControlthen 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 segmentControland your code will work that time . Thanks

你必须做的事情拿走你的IBOutlet(财产)segmentedControl然后我会为你工作。不要在按钮方法中创建新对象。当您制作 IBOutlet 时,它将与 at 链接,segmentControl届时您的代码将起作用。谢谢