Objective c 在Cocoa Touch中手动添加按钮后如何隐藏按钮?

Objective c 在Cocoa Touch中手动添加按钮后如何隐藏按钮?,objective-c,cocoa-touch,uibutton,Objective C,Cocoa Touch,Uibutton,我在for循环中手动添加了按钮,之后,如何隐藏或更改按钮和隐藏标签 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(80.0, 170, 150.0, 30.0); [button setTitle:@"My Button" forState:UIControlStateNormal]; [button addTarget:self action:@se

我在for循环中手动添加了按钮,之后,如何隐藏或更改按钮和隐藏标签

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:@"My Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UILabel *lblFileName = [[UILabel alloc] init];
lblFileName.text = [[objectArray objectAtIndex:i] valueForKey:@"fileName"];
lblFileName.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblFileName];

-(IBAction)myAction:(id)sender
{
    // hide the button or change title button and hide label
}

如果在循环的
中添加
按钮和标签
,请尝试以下解决方案

button.tag = i + 1;//0 is default tag for all views.
lblFileName.tag = i + 1;

-(IBAction)myAction:(id)sender
{
    UIButton *btn= (UIButton *)sender;
    [btn setHidden:YES];// hide the button
    btn.titleLabel.text = [[objectArray objectAtIndex:[sender tag]] valueForKey:@"fileName"];

     //Get the label of selected button using button tag.
    [[self.view viewWithTag:[sender tag]] setHidden:YES];

}
在myAction中:(id)发送者是您的按钮

- (IBAction)myAction:(id)sender {
       [sender setHidden:YES];
}
但是在这种情况下,您不能再让按钮可见,因为您没有对它的引用。我认为最好为按钮创建一个属性

-(IBAction)btn:(id)sender
{

   btn.hidden=YES;
}

试试这个。

其他回答者已经说明了
发送者
是如何成为你想要的按钮的,但是如果你想用不同的方法访问按钮(例如,如果你想让它再次可见),你需要在创建按钮的对象上创建一个属性

为此,请将其放入类
@接口中:

@property (strong) UIButton *myButton;
并在您的
@实现中合成它

@implementation WhateverClass
@synthesize myButton = _myButton;
然后可以将属性设置为自定义按钮:

self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[self.myButton setTitle:@"My Button" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
并在任何地方(创建后)访问它,如下所示:


如Girish所说,为每个按钮和标签提供唯一的标签。没有两个标签不应该是相同的

假设您有10个按钮和10个标签。然后给按钮1到10贴标签,给标签11到20贴标签

然后在您的方法上,您可以使用相应的标记访问任何标签或按钮

UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];

谢谢。若标签,那个么点击按钮后如何更改它。UILabel*lblFileName=[[UILabel alloc]init];lblFileName.text=[[objectArray objectAtIndex:i]valueForKey:@“fileName”];lblFileName.textAlignment=NSTextAlignmentCenter;objectArray是包含您在问题中提到的标签标题的dict,用于设置标签文本。如果我想在单击按钮后隐藏标签,请编辑您的问题并准确解释您想要的内容。如果单击第一个按钮,然后单击错误,如果单击另一个按钮,则不存在问题不能像这样,我的代码在for循环中,如果100,如何创建100个buttonOkay,这可能是您应该在问题中说明的内容。如果这就是你正在做的,@Girish的答案可能最有效。你想隐藏所有标签或与特定按钮相关的标签。使用我更新的答案隐藏与特定按钮相关的标签。
-(IBAction)myAction:(id)sender
{
    [self.myButton setTitle:@"Pressed!" forState:UIControlStateNormal];
    [self.myButton setHidden:YES];
}
UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];