访问附加到Custom Cells头文件的按钮

时间:2014-03-13 14:05:32

标签: ios objective-c uitableview

我有一个uitableviewcontroller(mainTVC),其中包含几个自定义的uitableviewcells。

uitableviewcontroller是swrevealview的前屏幕(https://github.com/John-Lluch/SWRevealViewController) - 我需要将revealtoggle功能附加到其中一个自定义单元格中的按钮,以在后视图中显示消息屏幕。

该按钮作为IB Outlet附加在自定义单元头文件中,如下所示 -

msgPost.h:

@property (weak, nonatomic) IBOutlet UIButton *msgsBtn; 

自定义单元格msgPost.h文件随后包含在mainTVC.m文件中 - 我想编写一个在单击按钮时触发的函数 - 基本上它需要执行以下操作 -

- (void)buttonImpMsg
   {
        NSLog(@"Back Button Pressed!");
        [self.revealViewController revealToggle:nil];
        [self hide];
    }

但我不确定如何从mainTVC.m文件创建访问msgsBtn属性 - 任何提示?

修改

基本上我需要知道的是如何从父uitableview控制器访问按钮单击操作 - (该按钮作为ibOutlet附加到导入到uitableviewcontrollers m文件的自定义单元.h文件中)

2 个答案:

答案 0 :(得分:1)

可以通过选择器完成。

在自定义单元格类定义(CustomCell.h文件)中定义选择器属性:

@property SEL buttonPressedSelector
@property id buttonPressedTarget

将此项放入您的自定义类,方法,按下按钮时调用该方法:

-(IBAction)buttonPressed:(id)sender
{
    [self.buttonPressedTarget performSelector:self.buttonPressedSelector];
}

将其放入视图控制器:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell"];
    // Your code...
    cell.buttonPressedSelector = @selector(cellBtnPressed);
    cell.buttonPressedTarget = self;
}

-(void) cellBtnPressed
{
    NSLog(@"Button Pressed Event in your View Controller.");
    // Your code is here
}

在我的示例中,CustomCell是您的msgPost,View Controller是mainTVC。

答案 1 :(得分:0)

您需要将按钮链接到方法,方法是按住Ctrl键单击Interface Builder中的按钮,然后从按钮拖动到“文件所有者”图标。