Question

I have a bar button and the action is as follows. Here I need to show an image when it is clicked first and I need to hide that when it is clicked next.

My problem is I am able to show the image but I am unable to hide it. Funny thing is its executing else part of action but still its not hiding the image.please help me out.

- (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;
       }
}

Thanks in advance.

Was it helpful?

Solution

i think flag is a BOOL variable so in viewDidLoad set flag=NO; and define ImageView Variable UIImageView *shadowView; in to .h file property and synthesize in .m class then in to your IBAction Method set like:-

- (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;
       }
}

OTHER TIPS

As shadowView is local variable in the 'else' scope you access nil value. To access shadowView that you actually added as a subview you should declare it as and ivar or property and hold a strong reference to it.

Example.

@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;
   }
}

In fact you don't need the flag ivar now then. You can extract image view creation code somewhere else as well and in the action method just handle one thing - showing/hiding.

The shadowView is an Local variable. Can you allocate the shadowView in ViewDidLoad() method as currently every time if condition is true the imageView will be allocated.

@interface MyClass ()
   @property (nonatomic, strong) UIImageView *shadowView;
@end

- (void)viewDidLoad {
    shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top