Button click not working in iOS

1.8k views Asked by At

I am building an ANE which will start a new View which contains a label and button. I was successful in adding view as bundle and view controller in my .a file.

I am starting my view as a subview. And later when I click button, it should do some stuffs and change the label text. I can display the view using below code but my button click event is not working.

--- Code to start view ---

My ViewController class name is 'VideoViewController' and bundle containing xib name is 'ViewBundle.bundle'

UIApplication *app = [UIApplication sharedApplication];

UIViewController *myViewController;



NSBundle * mainBundle = [NSBundle mainBundle];

NSString * pathToMyBundle = [mainBundle pathForResource:@"ViewBundle" ofType:@"bundle"];

NSAssert(pathToMyBundle, @"bundle not found", nil);

NSBundle *bundle = [NSBundle bundleWithPath:pathToMyBundle];

myViewController = [[VideoViewController alloc] initWithNibName:nil bundle:bundle];



[app.keyWindow addSubview:myViewController.view];

--- VideoViewController.h ---

#import <UIKit/UIKit.h>



@interface VideoViewController : UIViewController



@property (strong, nonatomic) IBOutlet UILabel *textLabel;

@property (strong, nonatomic) IBOutlet UIButton *clickBtn;

- (IBAction)BtnTapped:(id)sender;

- (IBAction)BtnTappped:(UIButton *)sender;



@end

--- VideoViewController.m ---

#import "VideoViewController.h"



@interface VideoViewController ()



@end



@implementation VideoViewController



#pragma mark - UIViewController



- (void)viewDidLoad {

    [super viewDidLoad];



    [self logMessage:@"From did load"];

    [_clickBtn setUserInteractionEnabled:YES];

    [_clickBtn setTitle:@"Click Here" forState:UIControlStateNormal]; // button text changes here

}



#pragma mark - Public



- (IBAction)BtnTapped:(id)sender {

    [_textLabel setText:@"Btn Tapped"];

}



- (IBAction)BtnTappped:(UIButton *)sender {

    [_textLabel setText:@"Btn Tappped"];

}



#pragma mark - Private



- (void)logMessage:(NSString *)msg {

    NSLog(@"%@", msg);

    [_textLabel setText:msg];

}



@end

I tried to give touch event programmatically, but still button click event doesn't work.

Can anyone please help to sort out this issue?

2

There are 2 answers

0
Ijas Ahamed N On BEST ANSWER

Issue was with view initialisation. Replace [app.keyWindow addSubview:myViewController.view]; with [app.keyWindow.rootViewController presentViewController:myViewController animated:false completion:nil]; solved my issue.

3
Jater On

Please check your clickButton is on its superView