Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iAction与动态添加的UIbuttons一起使用_Iphone_Uibutton - Fatal编程技术网

Iphone iAction与动态添加的UIbuttons一起使用

Iphone iAction与动态添加的UIbuttons一起使用,iphone,uibutton,Iphone,Uibutton,大家晚上好 我在视图中动态添加了一些UIButtons,当然我还有一个iAction来处理按钮事件 问题是:如果我只知道(id)发送器和按钮数组,那么如何检测按下了哪个按钮 按钮从来都不一样,每个按钮都有不同的行为。当我想使用静态按钮并通过IB连接它们时,我会使用如下方式: -(IBAction)doSomething:(id)sender { if(sender == button1) dosomething; if(sender == button2)

大家晚上好

我在视图中动态添加了一些UIButtons,当然我还有一个iAction来处理按钮事件

问题是:如果我只知道(id)发送器和按钮数组,那么如何检测按下了哪个按钮

按钮从来都不一样,每个按钮都有不同的行为。当我想使用静态按钮并通过IB连接它们时,我会使用如下方式:

-(IBAction)doSomething:(id)sender
{
    if(sender == button1)
        dosomething;
    if(sender == button2)
        dosomething else;
    if(sender == button3)
        dosomething3;
}
在我的例子中,这不起作用,因为没有button1、button2、button3,而是一个可变的按钮数组,这些按钮与分配给它的按钮具有相同的名称。按钮

我试着用上面的方法,但没有成功,我也试着得到一个按钮的标签,但我没有什么可比的

我非常感谢你的帮助

真诚地
L_Sonic PS Dynamicly意味着我在运行时随机创建按钮,如下所示

-(void)configActionSheetView
{
    buttonView = [[UIView alloc]initWithFrame:CGRectMake(0.0,460, 60, 480)];
    [buttonView setBackgroundColor:[UIColor blackColor]];
    [buttonView setAlpha:0.6];
    for (int i = 0 ;i<[buffButtons count];i++)
    {
        UIButton *customButton = [buffButtons objectAtIndex:i];
        customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //UILabel   *customLabel = [[UILabel alloc]init];
        //[customButton setTag:(i)+11];
        [customButton addTarget:self action:@selector(activateBuffEffect:) forControlEvents:UIControlEventTouchUpInside];
        [customButton setAlpha:1.0];
        customButton.frame = CGRectMake(8.0, 5+(50*i), 44.0, 44.0);
        [customButton setTitle:nil forState:UIControlStateNormal];
        buttonView.frame = CGRectMake(0, 460, 60, 50+(44*(i+1)));
        [buttonView addSubview:customButton];
    }
}
-(无效)配置操作表视图
{
buttonView=[[UIView alloc]initWithFrame:CGRectMake(0.0460,60480)];
[buttonView setBackgroundColor:[UIColor blackColor]];
[按钮视图设置Alpha:0.6];
对于(int i=0;i当您被“动态添加”时,我假设您的意思是它们是从某段代码创建的。既然所有按钮都指向不同的东西,并且您知道某个按钮应该做什么,为什么不向不同的按钮添加不同的操作呢

UIButton *myCreatedButton = [[UIButton alloc] init];
[myCreatedButton addTarget:self 
                    action:@selector(doSomething:) 
          forControlEvents:UIControlEventTouchUpInside];

UIButton *myOtherCreatedButton = [[UIButton alloc] init];
[myOtherCreatedButton addTarget:self 
                         action:@selector(doSomethingElse:) 
               forControlEvents:UIControlEventTouchUpInside];
在上面的代码中,target(设置为self)是找到要运行的方法的类,action是要运行的方法,controlEvent是导致该方法运行的原因

如果您这样做,您会将代码拆分为以下不同的方法(不需要在标题中指定):

这样,您就不需要知道按下了什么按钮,因为正确的代码将被调用。此外,如果您需要更改某些按钮的代码,它将使它更干净。

找到了它

-(IBAction)buttonTapped:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSLog(@"tapped: %@", btn.titleLabel.text);
    [self anotherIBAction:sender];
}
现在我可以从btn:D获取标签了
谢谢!

为什么不在按钮上添加一个标签,然后在选择器功能中从(id)发送器获取标签号?

按钮的可变数组来自何处?当您创建这些按钮时,您不能给它们一个标签吗?可能是创建的顺序?按钮有一个标签!但是我必须将标签与之进行比较吗?
-(IBAction)buttonTapped:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSLog(@"tapped: %@", btn.titleLabel.text);
    [self anotherIBAction:sender];
}