我在一个循环中创建了8个按钮,这些按钮排成4行2列, 点击时显示我可以使用的单个标签 开关。但是我似乎无法在我之后隐藏所有按钮 完成了他们。我想点击一个spearate按钮 将删除隐藏或全部删除。
·H
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UIButton *ventButtons;
@end
//UIButton *ventButtons; // <--- this works with or with out does not affect outcome..?? Do i need this in xcode 6?
的.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize ventButtons;
我的代码
int x = 260;
int y = 200;
for (int j = 0; j < 8; j++) { // create 8 buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self setVentButtons:button];
[button setTag:j];
button.frame = CGRectMake(x, y, 20, 20);
if (j == 1 || j == 3 || j == 5 || j == 7) {
[button setTitle: @"▶️" forState: UIControlStateNormal];
y = y + 20;
x = x - 40;
} else {
[button setTitle: @"◀️" forState: UIControlStateNormal];
y = y - 10;
}
[button addTarget:self action:@selector(getVentValue:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:ventButtons];
y=y+10;
x=x+20;
当运行时,所有按钮在单击时显示单个标签,但是如果 我尝试使用选择器隐藏或删除标记的按钮 不使用下面的代码隐藏标记按钮,没有任何反应。
NSLog(@"Vent Button tag: %ld",(long)[sender tag]);
[[self.ventButtons viewWithTag:3] setHidden:YES];
我也试过删除这样的按钮,它只删除了 创建了最后一个按钮,即按钮标签7。
- (void)getVentValue:(id)sender
{
NSLog(@"Vent Button tag: %ld",(long)[sender tag]);
[self.ventButtons removeFromSuperview];
self.ventButtons = nil;
..
我想我需要使用数组,但我不知道如何设置它。 我希望有一种方法可以使用属性变量并选择 via标签内的所需按钮。任何帮助将不胜感激。
答案 0 :(得分:2)
此代码([[self.ventButtons viewWithTag:3] setHidden:YES];
)无效,因为您在所选/有效viewWithTag
button
这样做:([[self.view viewWithTag:3] setHidden:YES];
)将完成工作..
但是如果你想使用数组,你可以将它与循环一起添加:
NSMutableArray *arrayButtons = [[NSMutableArray alloc] init]; // global variable
for (int j = 0; j < 8; j++)
{
..
[arrayButtons addObject:button];
// or
//to keep the most recent created button in index 0
[arrayButtons insertObject:button atIndex:0];
// in the array button arrangement would be: ["button8", "button7", "button6"..];
..
}
并将其隐藏起来:
//to avoid warning: cast the kind of class
UIButton *button = (UIButton *)[arrayButtons objectAtindex:3];
button.hidden = YES;
// or simply
[[arrayButtons objectAtIndex:3] setHidden:YES];
另一个问题是:
您正在添加[self.view addSubview:ventButtons];
这是什么?一系列按钮?或者也许是所有按钮的容器?
for (int j = 0; j < 8; j++) { // create 8 buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// [self setVentButtons:button]; ? what is this ?
[button setTag:j];
..
[button addTarget:self action:@selector(getVentValue:) forControlEvents:UIControlEventTouchUpInside];
// do this instead since you already set everything for the button
[self.view addSubview:button]; // previous code: [self.view addSubview:ventButtons];
您也可以使用for-statement
for (int i = 0; i < 8; i++)
[[self.view viewWithTag:i] setHidden:YES];
还有:
- (void)getVentValue:(id)sender
{
// You are logging the tag of the sender
NSLog(@"Vent Button tag: %ld",(long)[sender tag]);
// and you are working with this `self.ventButtons`
// [self.ventButtons removeFromSuperview];
// self.ventButtons = nil;
..
Why not?
UIButton *button = (UIButton *)sender;
button.hidden = YES;
//or
[(UIButton *)sender setHidden:YES];
注意:强>
但我建议您继续使用标签,这是您实施的最佳选择。记忆明智的选择。
希望这会对你有所帮助,干杯...... :)
答案 1 :(得分:1)
您不需要数组,按钮已添加到视图中,您可以根据需要进行迭代。
但是在这种情况下,因为你想要在点击时删除按钮,所以当调用getVentValue方法时,你会得到对它的引用,即“sender”,所以你唯一需要做的就是:
- (void)getVentValue:(id)sender {
NSLog(@"Vent Button tag: %ld",(long)[sender tag]);
// Button that was tapped
UIButton *button = (UIButton *)sender;
[button removeFromSuperview];
// or if you want to hide it then do
// button.hidden = YES;
}
另外一件事,你不需要那个属性“ventButtons”,你可以摆脱它,因为你正在迭代并添加每个奇数的按钮,那么你可以在循环中使用mod运算符:
int x = 260;
int y = 200;
for (int j = 0; j < 8; j++) { // create 8 buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTag:j];
button.frame = CGRectMake(x, y, 20, 20);
if (j % 2 == 1) {
[button setTitle: @"▶️" forState: UIControlStateNormal];
y = y + 20;
x = x - 40;
} else {
[button setTitle: @"◀️" forState: UIControlStateNormal];
y = y - 10;
}
[button addTarget:self action:@selector(getVentValue:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
y += 10;
x += 20;
}
这应该适合你。