按钮点击在 iOS 中不起作用
Button click not working in iOS
我正在构建一个 ANE,它将启动一个包含标签和按钮的新视图。我成功地在我的 .a 文件中添加视图作为捆绑包和视图控制器。
我正在以子视图的形式启动我的视图。稍后当我单击按钮时,它应该做一些事情并更改标签文本。我可以使用下面的代码显示视图,但我的按钮单击事件不起作用。
--- 开始查看的代码 ---
我的 ViewController class 名称是 'VideoViewController' 并且包含 xib 名称的包是 '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
我尝试以编程方式提供触摸事件,但按钮点击事件仍然不起作用。
谁能帮忙解决这个问题?
请检查您的 clickButton 在它的 superView 上
问题与视图初始化有关。将 [app.keyWindow addSubview:myViewController.view];
替换为
[app.keyWindow.rootViewController presentViewController:myViewController animated:false completion:nil];
解决了我的问题。
我正在构建一个 ANE,它将启动一个包含标签和按钮的新视图。我成功地在我的 .a 文件中添加视图作为捆绑包和视图控制器。
我正在以子视图的形式启动我的视图。稍后当我单击按钮时,它应该做一些事情并更改标签文本。我可以使用下面的代码显示视图,但我的按钮单击事件不起作用。
--- 开始查看的代码 ---
我的 ViewController class 名称是 'VideoViewController' 并且包含 xib 名称的包是 '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
我尝试以编程方式提供触摸事件,但按钮点击事件仍然不起作用。
谁能帮忙解决这个问题?
请检查您的 clickButton 在它的 superView 上
问题与视图初始化有关。将 [app.keyWindow addSubview:myViewController.view];
替换为
[app.keyWindow.rootViewController presentViewController:myViewController animated:false completion:nil];
解决了我的问题。