无法让UIButton以编程方式调用iOS中的操作

时间:2016-12-08 19:17:06

标签: ios objective-c uibutton

我无法启动按钮来触发关联的方法。我没有使用IB或类似的东西(所有程序)。 _mainViewController在自定义初始化期间传入,只是为了澄清(尽管这应该与按钮无关)。请参阅我的代码剪辑:

viewcontroller.h

#import <UIKit/UIKit.h>

@interface CSNWZSSViewController : UIViewController

- (void)doneButtonPressed;

@property UIViewController *mainViewController;
@property UIButton *doneButton;

@end

viewcontroller.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mainViewController.view.frame.size.width, 45)];
    headerView.backgroundColor = [UIColor darkGrayColor];

    _doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height)];
    [_doneButton addTarget:self
                   action:@selector(doneButtonPressed)
         forControlEvents:UIControlEventTouchUpInside];
    [_doneButton setTitle:@"Done" forState:UIControlStateNormal];
    _doneButton.tintColor = [UIColor whiteColor];
    _doneButton.backgroundColor = [UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
    [headerView addSubview:_doneButton];

    CGRect headerLabelFrame = CGRectMake(5, 5, headerView.frame.size.width - 105, headerView.frame.size.height - 5);
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerLabelFrame];
    headerLabel.text = @"test header";
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font = [UIFont systemFontOfSize:20.0f];
    headerLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [headerView addSubview:headerLabel];

    [self.view addSubview:headerView];
}

- (void)doneButtonPressed
{
    NSLog(@"button pressed");
}

使用buttonWithType init和显式userInteractionEnabled

更新10/9/16
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_doneButton addTarget:self
               action:@selector(doneButtonPressed)
     forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:@"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor blackColor]; //[UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
_doneButton.frame = CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height);
_doneButton.userInteractionEnabled = YES;
[headerView addSubview:_doneButton];

注意12/9/16 _mainViewController对象在这里传递:

CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];

使用matt帮助的解决方案(见下文) 由于缺少添加新的视图控制器,上下文变得混乱。

CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController addChildViewController:CSNWZSSVC];
[self.viewController.view addSubview:CSNWZSSVC.view];

2 个答案:

答案 0 :(得分:2)

  

在自定义init

期间传入_mainViewController

我猜这是问题所在 - 你对这个视图控制器管理不善,而且它没有正确地成为视图控制器层次结构的一部分,事实上甚至可能已经不存在了,所以那里没有人发送按钮动作消息。

修改是的,现在你发布了更多代码,我显然是对的:

CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];

这是完全错误的行为。您不能随意向您的视图添加另一个视图控制器的视图。有一个严格的舞蹈来管理儿童视图控制器,你没有做舞蹈。

答案 1 :(得分:0)

尝试:

UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];

然后设置你的框架。