Skip to main content
ijasnahamed
Participant
May 31, 2018
Answered

iOS - Button click event not workng

  • May 31, 2018
  • 1 reply
  • 452 views

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

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

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

- (IBAction)BtnTapped:(id)sender;

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

@8978565

--- VideoViewController.m ---

#import "VideoViewController.h"

@Interface VideoViewController ()

@8978565

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

}

@8978565

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

Can anyone please help to sort out this issue?

This topic has been closed for replies.
Correct answer ijasnahamed

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

1 reply

ijasnahamed
ijasnahamedAuthorCorrect answer
Participant
May 31, 2018

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