Highlighted

iOS - Button click event not workng

Community Beginner ,
May 31, 2018

Copy link to clipboard

Copied

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?

Correct answer by ijasnahamed | Community Beginner

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

TOPICS
Development

Views

242

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

iOS - Button click event not workng

Community Beginner ,
May 31, 2018

Copy link to clipboard

Copied

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?

Correct answer by ijasnahamed | Community Beginner

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

TOPICS
Development

Views

243

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 31, 2018 0
Community Beginner ,
May 31, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2018 0