【问题标题】:Button click not working in iOS按钮单击在 iOS 中不起作用
【发布时间】:2018-11-10 06:42:25
【问题描述】:

我正在构建一个 ANE,它将启动一个包含标签和按钮的新视图。我成功地在我的 .a 文件中添加视图作为包和视图控制器。

我将视图作为子视图开始。稍后当我单击按钮时,它应该做一些事情并更改标签文本。我可以使用下面的代码显示视图,但我的按钮单击事件不起作用。

--- 开始查看的代码 ---

我的 ViewController 类名称是“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

我尝试以编程方式提供触摸事件,但按钮单击事件仍然不起作用。

谁能帮忙解决这个问题?

【问题讨论】:

    标签: ios uiviewcontroller air ane touch-up-inside


    【解决方案1】:

    请检查您的 clickButton 是否在其 superView 上

    【讨论】:

    • 如何查看超级视图中的按钮?
    • 使用视图调试 - 捕获视图层次结构
    • @Jater 请在答案中添加更多描述,以便用户轻松理解
    【解决方案2】:

    问题在于视图初始化。将 [app.keyWindow addSubview:myViewController.view]; 替换为 [app.keyWindow.rootViewController presentViewController:myViewController animated:false completion:nil]; 解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2018-05-02
      • 2016-06-07
      • 2014-03-24
      • 1970-01-01
      相关资源
      最近更新 更多