Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Objective c 目标C:使用标签_Objective C_Tags_Uibutton - Fatal编程技术网

Objective c 目标C:使用标签

Objective c 目标C:使用标签,objective-c,tags,uibutton,Objective C,Tags,Uibutton,过去几天我一直在研究这个问题,但我想不出答案。我有很多按钮做同样的事情(单击时消失)。我用自己的标签定义了每一个,但如何确定按下了哪一个 -(IBAction) tapBrick{ int x = brick.tag; NSLog(@"%d", x); //remove last brick [brick removeFromSuperview]; //add to score count++; NSString *scoreString = [NSString stringWithFor

过去几天我一直在研究这个问题,但我想不出答案。我有很多按钮做同样的事情(单击时消失)。我用自己的标签定义了每一个,但如何确定按下了哪一个

-(IBAction) tapBrick{
int x = brick.tag;
NSLog(@"%d", x);


//remove last brick
[brick removeFromSuperview];

//add to score
count++;
NSString *scoreString = [NSString stringWithFormat:@"%d", count];
score.text = scoreString;

//determine x y coordinates
int xPos, yPos;
xPos = arc4random() % 250;
yPos = arc4random() % 370;
}


-(void) produceBricks {
//determine x y coordinates
int xPos, yPos;
xPos = arc4random() % 250;
yPos = arc4random() % 370;

//create brick
brick = [[UIButton alloc] initWithFrame:CGRectMake(xPos,yPos + 60,70,30)];  
[brick setBackgroundColor:[UIColor blackColor]];
[brick setTag:i];
[brick addTarget:self action:@selector(tapBrick) forControlEvents:UIControlEventTouchUpInside];
i++;
[self.view addSubview:brick];


}

计时器每2秒调用一次生成砖块

Chris,如果您需要做的只是识别已按下的按钮,只需更改方法声明以接受
sender
参数,调用者(在本例中为
UIButton
)将提供对自身的引用。创建一个
ui按钮
指针,您将能够访问被按下按钮的标签

-(void) tapBrick:(id)sender {
    //this is the button that called your method.
    UIButton *theButton = (UIButton *)sender;

    int tag = theButton.tag;
    NSLog(@"%d", tag);

    [theButton removeFromSuperview];

    //rest of code  
}

(顺便说一句,由于您正在使用代码创建按钮,因此不需要声明
iAction
iAction
的返回值与
void
相同,只是它提示界面生成器您将把一些
IBOutlet
连接到该特定方法。),如果您只需要识别已按下的按钮,只需更改方法声明以接受
发送方
参数,调用方(在本例中为
UIButton
)将提供对自身的引用。创建一个
ui按钮
指针,您将能够访问被按下按钮的标签

-(void) tapBrick:(id)sender {
    //this is the button that called your method.
    UIButton *theButton = (UIButton *)sender;

    int tag = theButton.tag;
    NSLog(@"%d", tag);

    [theButton removeFromSuperview];

    //rest of code  
}

(顺便说一句,由于您正在使用代码创建按钮,因此不需要声明
iAction
iAction
的返回值与
void
相同,只是它提示界面生成器您将把某个
IBOutlet
连接到该特定方法。)

我刚刚这样做了,但它会导致运行时错误。我是否将[brick Removefrom SuperView]更改为按钮或其他内容?是的,完全正确--让我填写方法的其余部分。太棒了,谢谢!所以如果我理解正确的话,通过将它设为发送器,它只会通过按下哪个按钮的标签发送?实际上,它会通过指针发送到按钮本身。我们使用
int标记
赋值提取标记,就像我们从刚刚创建的对象提取标记一样。(你知道你有一个指针是因为
id
类型。
id
只是指任何类型的对象。)是我还是NSLog错了。我认为x变量实际上应该是之前创建的标记变量。我只是这样做了,但它会导致运行时错误。我是否将[brick Removefrom SuperView]更改为按钮或其他内容?是的,完全正确--让我填写方法的其余部分。太棒了,谢谢!所以如果我理解正确的话,通过将它设为发送器,它只会通过按下哪个按钮的标签发送?实际上,它会通过指针发送到按钮本身。我们使用
int标记
赋值提取标记,就像我们从刚刚创建的对象提取标记一样。(你知道你有一个指针是因为
id
类型。
id
只是指任何类型的对象。)是我还是NSLog错了。我认为x变量实际上应该是之前创建的标记变量。您在上一个线程中收到了关于如何在没有标记的情况下执行此操作的答案:对于记录,标记是UIView类(及其派生类)的一个功能,而不是一个目标C。您收到了关于如何在没有标记的情况下执行此操作的答案:对于记录,标记是UIView类(及其派生类)的一个特性,而不是一个Objective C特性。