3

我在自定义表格视图单元格中有一些 UIButtons,当按下时我希望它们在单元格所在的表格视图所在的视图控制器中调用 segues。我现在正在做的是声明这样的操作:-(无效)行动;

在表视图所在的类中。然后我从单元格中调用该操作,如下所示:

ViewController *viewController = [[ViewController alloc] init];
[viewController action];

但是,当调用该操作时,它表示视图控制器没有这样的 segue,我知道这是不正确的。从视图控制器本身调用“动作”可以完美地工作,而不是从单元格中调用。

此外,这是执行 segue 的代码:

-(void)action {
     [self performSegueWithIdentifier:@"action" sender:self];
}

我怎样才能使这项工作?

任何建议表示赞赏。

更新

我只是试图在单元格上设置一个代表,如下所示:

Cell的头文件:@class PostCellView;

@protocol PostCellViewDelegate
-(void)action;
@end

@interface PostCellView : UITableViewCell <UIAlertViewDelegate, UIGestureRecognizerDelegate>

@property (weak, nonatomic) id <PostCellViewDelegate> delegate;

在单元格的主文件中,我将操作称为:[self.delegate action];

在承载表格视图的视图的标题中,我像这样导入单元格:#import "PostCellView.h"

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, PostCellViewDelegate>

然后在该视图的主文件中,我有单元格操作:

-(void)action {

    [self performSegueWithIdentifier:@"action" sender:self];

}

但我检查过,该动作从未执行过。当我从单元格调用操作时我记录了,我在操作本身上放置了一个 NSLog,从单元格调用它的日志有效但不是操作。

4

2 回答 2

1

我认为您的问题在于ViewController *viewController = [[ViewController alloc] init]; [viewController action]; 您正在启动一个新的 ViewController 并且没有获取单元所在的当前 ViewController。

也许您可以在单元格上设置一个委托,以便单元格具有对 viewController 的引用

于 2012-10-06T22:37:56.673 回答
0

创建按钮并以编程方式调用其处理程序的简单方法是

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 80, 40)];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button]; //self.view is the view on which you want to add button

并且它的处理程序应该在你的实现文件中这样定义:

-(void)buttonClick:(id)sender{
    NSLog(@"button clicked");
    // your code on click of button 
}

您可以查看iOS 文档

希望这将帮助您找出您的问题。:)

于 2012-10-06T21:45:30.420 回答