1

Forgive me if this is trivial, I am still honing my programing skills. I am trying to set this button as a target. Should be easy but I dont know why it's not working! I inserted a NSLog to test and the method is not being called! Thanks for your help.

//ShareView.h

@property (strong,nonatomic) UIButton *cancelBtn;


//ShareView.m
- (id)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code

            UIImage *shareImage = [UIImage imageNamed:@"shareBox.png"];
            [self setFrame:CGRectMake(10, 170, shareImage.size.width, shareImage.size.height)];

            self.shareIV = [[UIImageView alloc] initWithImage:shareImage];
            self.shareIV.userInteractionEnabled = YES;

            [self addSubview:self.shareIV];  

            [self shareBtnsInit];
            [self.shareIV addSubview:self.cancelBtn];

            self.userInteractionEnabled = YES;
        }
        return self;
}


-(void)shareBtnsInit{

        UIImage *cancelImg = [UIImage imageNamed:@"cancel27.png"];
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn setImage:cancelImg forState:UIControlStateNormal];
        [self.cancelBtn setFrame:CGRectMake(277, 3, cancelImg.size.width, cancelImg.size.height)];

}

//MainViewController.m

-(IBAction)settingsButtonPressed:(id)sender{
    self.shareVC = [[ShareView alloc] initWithFrame:CGRectMake(0, 0, 0,0)];
    [self.view addSubview: self.shareVC];

    [self.shareVC.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];

}

-(IBAction)settingsCancel:(id)sender{
    NSLog(@"TEST!!!");
    [self.shareVC removeFromSuperview];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ShadyBaker
  • 105
  • 1
  • 3
  • 8
  • Could you try inserting something like `NSLog(@"\n%@", [self.shareVC performSelector:@selector(recursiveDescription)]);` at the end of your `settingsButtonPressed:` method? – Isaac May 27 '14 at 17:52
  • 1
    If you don't even see that logging output, then your `settingsButtonPressed:` method isn't getting called. – Isaac May 27 '14 at 18:25
  • You were right. Figured it out! Thank you Isaac! – ShadyBaker May 27 '14 at 20:12

3 Answers3

1

Based on comments, your settingsButtonPressed: method wasn't being called, so the button you were looking for the action on was never being set up.

Isaac
  • 10,668
  • 5
  • 59
  • 68
0

Try adding [self.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside]; to your shareBtnsInit method.

Here's more information on making UIButtons programatically: How do I create a basic UIButton programmatically?

Community
  • 1
  • 1
  • I wanted to initialize the button in one class (ShareView) and set the target in another(MainViewController). Is that doable? – ShadyBaker May 27 '14 at 18:07
  • Yes. `-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents` is a method in the UIControl class. UIButton is a subclass of UIControl. If you change the target to the MainViewController the action will be called in that class. – Michael Selsky May 27 '14 at 18:55
0
[self.cancelBtn addTarget:MainViewController action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];

In ShareView you have to import class in which you want your event should get triggered. Here in ShareView you can import MainViewController class and can use it.

Ruchish Shah
  • 319
  • 1
  • 6