【问题标题】:UIBarButtonItem button not firingUIBarButtonItem 按钮未触发
【发布时间】:2014-10-11 17:40:19
【问题描述】:

我有一个带有 UINavigationController 的应用程序,其中包含一堆 UITableViewControllers,我会根据用户选择的内容换掉它们。所有的 UITableViewControllers 都有一个按钮“添加事件”。在大约一半的表格视图控制器上,按钮根本不会触发。对于我的生活,我无法弄清楚为什么。

这是一个不起作用的 init 函数的示例。有问题的按钮是“doneButton”。单击时它不会触发 addEvent()。

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle: UITableViewStyleGrouped];
    if (self) {
        // initialize toolbar
        toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake( 0, 0, 768, 44 )];
        titleLabel = [[UILabel alloc] initWithFrame: CGRectMake( 284, 4, 200, 35 )];
        titleLabel.text = @"End Repeat";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize: 20];
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        [toolbar addSubview: titleLabel];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Add Event" style: UIBarButtonItemStyleDone target: self action: @selector(addEvent)];
        NSArray *buttonArray = [NSArray arrayWithObjects: flexibleSpace, doneButton, nil];
        [toolbar setItems: buttonArray];
        [self.view addSubview: toolbar];

        // initialize date picker
        datePicker = [[UIDatePicker alloc] initWithFrame: CGRectMake( 0.0, 0.0, 768.0, 216.0 )];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [datePicker addTarget: self action: @selector(changeDate) forControlEvents: UIControlEventValueChanged];

        [self roundDatePicker];
        [self parseDone];
    }
    return self;
}

下面是一个有效的初始化函数示例:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle: UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
        caController = [[CAViewController alloc] init];
        selectedRow = 0;

        self.title = @"Calendar";

        self.toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake( 0, 0, 768, 44 )];
        titleLabel = [[UILabel alloc] initWithFrame: CGRectMake( 284, 4, 200, 35 )];
        titleLabel.text = @"Select Calendar";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize: 20];
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        [toolbar addSubview: titleLabel];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Add Event" style: UIBarButtonItemStyleDone target: self action: @selector(addEvent)];
        NSArray *buttonArray = [NSArray arrayWithObjects: flexibleSpace, doneButton, nil];
        [toolbar setItems: buttonArray];
        [self.view addSubview: toolbar];
    }
    return self;
}

【问题讨论】:

  • -(IBAction)addEvent:(id)sender 应该是方法,将您的选择器更改为@selector(addEvent:)
  • 将您的 addEvent 代码发布在不起作用的类中。
  • 我尝试更改为-(IBAction)addEvent:(id)sender,但没有成功。我的 addEvent 代码应该无关紧要,因为我只是在此处设置了一个断点,但这里是:
  • - (IBAction) addEvent: (id) sender { //[self parseDone]; AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UINavigationController *controller = (UINavigationController *)[delegate.splitController.viewControllers objectAtIndex: 0]; RootTableViewController *rootController = (RootTableViewController *)[controller.viewControllers objectAtIndex: 0]; [rootController parseDone]; }
  • 我还尝试为完成按钮的操作添加不同的方法。还是不会火。就好像按钮上有什么看不见的东西,所以我无法点击它。

标签: ios uibarbuttonitem


【解决方案1】:

2 个月的逐步重建我的应用程序,我终于找到了它!这挡住了我的按钮:

- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 1)];
}

在我的一生中,我无法弄清楚这段代码的作用,或者我为什么一开始就把它放在那里。我删除了它,现在一切似乎都正常了。

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多