执行由UITableViewCell调用的Segue [英] Perform a Segue Called by a UITableViewCell
问题描述
我在自定义表格视图单元格中有一些UIButton,按下这些按钮时,我希望它们在作为该单元格所在的表格视图宿主的视图控制器中调用segues。我现在要做的是声明这样的操作:
-(void)action;
I have some UIButtons in a custom table view cell that when pressed I want them to call segues in the view controller that is host to the table view that the cells are in. What I am doing now is declaring an action like this: - (void)action;
在表视图所在的类中。然后我从像这样的单元格中调用该动作: / p>
in the class that the table view is in. And then I am calling that action from the cell like this:
ViewController *viewController = [[ViewController alloc] init];
[viewController action];
但是,当调用动作时,它说对于我知道的视图控制器没有这样的保护是不正确的。从视图控制器本身调用 action非常有效,只是不能从单元格中调用。
However when the action is called it says that there is no such segue for the view controller which I know to be incorrect. Calling "action" from the view controller itself works perfectly, just not from the cell.
此外,这是执行segue的代码:
Also, here is the code to perform the segue:
-(void)action {
[self performSegueWithIdentifier:@"action" sender:self];
}
我该如何进行这项工作?
How can I make this work?
任何建议都会受到赞赏。
Any suggestions are appreciated.
更新:
我只是想在像这样的单元格上设置一个委托:
I just tried to set a delegate on the cell like this:
单元格的头文件:
@class PostCellView;
Cell's header file: @class PostCellView;
@protocol PostCellViewDelegate
-(void)action;
@end
@interface PostCellView : UITableViewCell <UIAlertViewDelegate, UIGestureRecognizerDelegate>
@property (weak, nonatomic) id <PostCellViewDelegate> delegate;
在单元格的主文件中,我将动作称为:
[self .delegate操作];
And in the main file for the cell I call the action like this: [self.delegate action];
,并在托管表视图的视图的标题中,我这样导入单元格:
#import PostCellView.h
and in the header for the view that hosts the Table View I import the cell like this: #import "PostCellView.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, PostCellViewDelegate>
然后在该视图的主文件中,我具有单元格操作:
Then in the main file for that view I have the cells action:
-(void)action {
[self performSegueWithIdentifier:@"action" sender:self];
}
但是我从未检查过该动作。我从单元格调用操作时记录了日志,然后在操作本身上放置了一个NSLog,并且从单元格调用该操作的日志有效,但操作无效。
But the action is never performed, I checked. I logged when I call the action from the cell and I put an NSLog on the action itself and the log for calling it from the cell worked but not the action.
推荐答案
我认为您的问题出在此 ViewController * viewController = [[ViewController alloc] init];中。
[viewController action];
您正在启动一个新的ViewController,但没有获取该单元格所在的当前ViewController。
I think your problem is with this ViewController *viewController = [[ViewController alloc] init];
[viewController action];
You are initiating a new ViewController and not getting the current one the cell is in.
也许您可以在单元格上设置一个委托,以便该单元格可以引用viewController
Maybe you can set a delegate on the cell so the cell has a reference to the viewController
这篇关于执行由UITableViewCell调用的Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!