Iphone 如何获取本案例中发件人的信息
我正在循环中创建Iphone 如何获取本案例中发件人的信息,iphone,events,uibutton,sender,Iphone,Events,Uibutton,Sender,我正在循环中创建ui按钮,如下所示 for(int i = 1; i <= count; i++){ if(i ==1){ UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)]; [btn1 setImage:image2 forState:UIControlStateNormal]; [self addS
ui按钮
,如下所示
for(int i = 1; i <= count; i++){
if(i ==1){
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
continue;
}
x = x + 40;
y = y + 50;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
我需要关于我在事件处理方法中单击了哪个按钮的信息。我需要对点击按钮的框架进行调整。如何更改此代码以获取有关发件人的信息。在按钮创建时添加标签,如
btn.tag=I编码>并像这样重新定义您的方法
-(void) buttonPressed:(id)sender{
// compare sender.tag to find the sender here
}
在按钮创建时添加标签,如btn.tag=i编码>并像这样重新定义您的方法
-(void) buttonPressed:(id)sender{
// compare sender.tag to find the sender here
}
您应该做的是在代码中添加以下行:
for(int i = 1; i <= count; i++)
{
if(i ==1){
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[btn1 setTag:i]; // This will set tag as the value of your integer i;
[self addSubview:btn1];
continue;
}
x = x + 40;
y = y + 50;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 setTag:i]; // also set tag here.
[btn2 addTarget:self action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
或者你也可以这样做。这样你就可以把发送者中的对象复制到这个uibton
对象中
-(void) buttonPressed: (id)sender
{
UIButton *tempBtn=(UIButton *) sender;
if(tempBtn.tag==1) // determine which button was tapped using this tag property.
{
// Do your action like..
tempBtn.backgroundColor=[UIColor blueColor];
tempBtn.alpha=0.5;
}
}
因此,在这个参数sender
中,它将保留您的ui按钮,您可以在该按钮上进行交互,并可以访问其属性,如标记、文本等。希望它有帮助。您应该做的是在代码中添加以下行:
for(int i = 1; i <= count; i++)
{
if(i ==1){
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[btn1 setTag:i]; // This will set tag as the value of your integer i;
[self addSubview:btn1];
continue;
}
x = x + 40;
y = y + 50;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(x , y, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 setTag:i]; // also set tag here.
[btn2 addTarget:self action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
或者你也可以这样做。这样你就可以把发送者中的对象复制到这个uibton
对象中
-(void) buttonPressed: (id)sender
{
UIButton *tempBtn=(UIButton *) sender;
if(tempBtn.tag==1) // determine which button was tapped using this tag property.
{
// Do your action like..
tempBtn.backgroundColor=[UIColor blueColor];
tempBtn.alpha=0.5;
}
}
因此,在这个参数sender
中,它将保留您的ui按钮,您可以在其上进行交互,并可以访问其属性,如标记、文本等。希望它能有所帮助。如果我在方法内部没有为sender获取任何属性,可能会有什么问题。您还需要将选择器更改为按钮按下:
(注意冒号)。如果发送者也总是知道的话,您可以将UIButton
而不是id
,这样您就不必强制转换。当我将我的(id)类型更改为(UIButton*)时它起作用了。但这不应该也适用于id
类型。是的,我也检查了.h。如果我没有在方法内部获取发件人的任何属性,可能会有什么问题。您还需要将选择器更改为按钮按下:
(注意冒号)。如果发件人也一直为人所知,您可以将ui按钮
替换为id
,这样您就不必强制转换。当我将我的(id)类型更改为(ui按钮*)它起作用了。但是这不应该也适用于id
类型。是的,我也检查了.h。你也需要更改选择器,以包括冒号。我没有在这里获得标记属性。其余都很好。发件人没有显示任何属性。你也需要更改选择器,以包括冒号。我没有得到e标记属性。其余都可以。发件人未显示任何属性。