Ios 单击工具栏按钮时图像未隐藏
我有一个酒吧按钮,动作如下。在这里,我需要在第一次单击时显示一个图像,然后在下一次单击时隐藏该图像 我的问题是我能够显示图像,但我无法隐藏它。有趣的是,它执行动作的其他部分,但仍然没有隐藏图像。请帮帮我Ios 单击工具栏按钮时图像未隐藏,ios,objective-c,uibutton,uibarbuttonitem,Ios,Objective C,Uibutton,Uibarbuttonitem,我有一个酒吧按钮,动作如下。在这里,我需要在第一次单击时显示一个图像,然后在下一次单击时隐藏该图像 我的问题是我能够显示图像,但我无法隐藏它。有趣的是,它执行动作的其他部分,但仍然没有隐藏图像。请帮帮我 - (IBAction)alerthelp:(id)sender { UIImageView *shadowView; if (!flag) { shadowView = [[UIImageView alloc] initWithFram
- (IBAction)alerthelp:(id)sender {
UIImageView *shadowView;
if (!flag)
{
shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
shadowView.image = [UIImage imageNamed:@"helphome.png"];
shadowView.opaque = YES;
shadowView.alpha = 0.8;
shadowView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:shadowView];
flag=YES;
}
else
{
shadowView.hidden=YES;
flag=NO;
}
}
提前感谢。shadowView是一个局部变量。是否可以按当前每次分配ViewDidLoad方法中的shadowView?如果条件为true,则将分配imageView
@interface MyClass ()
@property (nonatomic, strong) UIImageView *shadowView;
@end
- (void)viewDidLoad {
shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
}
希望这能有所帮助。因为shadowView是“else”范围内的局部变量,所以您可以访问nil值。
要访问实际添加为子视图的shadowView,应将其声明为和ivar或属性,并保留对其的强引用
例如
@interface MyClass ()
@property (nonatomic, strong) UIImageView *shadowView;
@end
...
- (IBAction)alerthelp:(id)sender {
if (!flag && !self.shadowView.hidden)
{
if(!self.shadowView) {
self.shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
self.shadowView.image = [UIImage imageNamed:@"helphome.png"];
self.shadowView.opaque = YES;
self.shadowView.alpha = 0.8;
self.shadowView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.shadowView];
}
self.shadowView.hidden = NO;
flag = YES;
}
else
{
self.shadowView.hidden = YES;
flag = NO;
}
}
事实上,你现在不需要ivar标志。您也可以在其他地方提取图像视图创建代码,在action方法中只需处理一件事-显示/隐藏。我认为flag是一个BOOL变量,因此在viewDidLoad set flag=NO;并定义ImageView变量UIImageView*shadowView;在to.h文件属性中,在.m类中进行合成,然后在iAction方法集中进行合成,如下所示:-
- (IBAction)alerthelp:(id)sender {
if (!flag)
{
shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
shadowView.image = [UIImage imageNamed:@"helphome.png"];
shadowView.opaque = YES;
shadowView.alpha = 0.8;
shadowView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:shadowView];
flag=YES;
}
else
{
shadowView.hidden=YES;
flag=NO;
}
}