我想向以编程方式添加的步进器添加动作,但是我不知道如何获得单击哪个步进器的信息。我有一个NSMutableArray,可以在其中更改值,但是我想知道单击了哪个步进器。这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.


for (int i = 0; i < count; i++) {

    //Here I create the stepper
    UIStepper *steperCode = [[UIStepper alloc] initWithFrame:CGRectMake(40, (i*30)+120, 300, 200)];
    steperCode.maximumValue = 1000;
    steperCode.minimumValue = 0;
    steperCode.stepValue=1.0;
    [self.view addSubview:steperCode];
}

最佳答案

尝试这个

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (int i = 0; i < count; i++)
    {
    UIStepper * stepperCode = [[UIStepper alloc] initWithFrame:CGRectMake(140, 60, 300, 200)];
    [steperCode addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    stepperCode.maximumValue = 10;
    stepperCode.minimumValue = 0;
    stepperCode.stepValue=2.0;
    stepperCode.tag = i;
    [self.view addSubview:stepperCode];
    }
}

- (IBAction)valueChanged:(UIStepper *)sender
{
   NSLog(@"Changed stepper num :%i",sender.tag);
}

关于ios - 以编程方式获取ID步进器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26233908/

10-13 05:00