我的视图控制器中有一个表视图,其中我有customTableViewCell。我有在customeview单元格中生成运行时的动态按钮。单击该按钮后,我想将持有我的表视图控制器和customTableViewCell的视图控制器推到另一个视图控制器,那么我该怎么做呢?在我的custometableviewcell.m
文件中,我有这个按钮。
CommentButton = [[UIButton alloc]init];
CommentButton.frame = CGRectMake(CommentLabel.frame.origin.x+CommentLabel.frame.size.width+5, 5, 110, 35);
CommentButton.titleLabel.font = [UIFont systemFontOfSize:12];
[CommentButton setTitle:@"Add Comment" forState:UIControlStateNormal];
CommentButton.backgroundColor = [UIColor clearColor];
[CommentButton addTarget:self action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];
以下是我在customtableviewcell.m
文件中的后通知,它将在点击的评论按钮上调用
-(IBAction)GoToComment:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"GoToComment" object:nil];
}
上面是我在NSNotification
上注册的按钮,它被称为以下方法
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(GotoComents)
name:@"GoToComment" object:nil];
上面一行是在我的viewcontroll的viewdidload中,在其中我有一个带有customtableviewcell的tableview。
并且从customtableview单元格,我发布了上面的notification,它将推送新的视图控制器
-(void)GotoComents
{
TableTextViewController *settingView=[[TableTextViewController alloc]initWithNibName:@"TableTextViewController" bundle:nil];
[self.navigationController pushViewController:settingView animated:YES];
}
现在,我刚刚用nsnotification中心完成了这项工作,它只是从我的customTableViewcell调用并调用viewcontroller方法。
但这样做对吗?如果不是,那么有人能帮助我吗?我该如何做到这一点。?请记住,我有一个生成运行时间的动态按钮。所以请有人指导我吗?
点击按钮事件
-(void)buttonAction:(UIButton*)sender
{
YourViewControllerVC *yvc=[YourViewControllerVC alloc] init];
[self.yournavigatioonController pushViewController:yvc animated:YES];
}
您可以将TableViewController子类声明为自定义单元格委托,在其中创建GoToComment方法,而不是
[CommentButton addTarget:self action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];
进行
[CommentButton addTarget:self.delegate action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];
为动态生成的按钮添加目标操作:
[myButton addTarget:targetObject action:@selector(actionMethod) forControlEvents:UIControlEventTouchUpInside];
然后为targetObject实现actionMethod,只要按下按钮就会调用它。在这个操作方法中,您需要以下代码:
UIViewController *viewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
您没有正确描述您的问题。。你可以用这种方式做你想做的事:
LoginViewController * login_view = [[ LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *Nav01 = [[UINavigationController alloc] initWithRootViewController:login_view];
Nav01.navigationBar.hidden = YES;
login_view.fromProfileView = FALSE;
[self presentModalViewController:Nav01 animated:YES];
我想你需要的是
- (UIViewController *)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
使用上面的代码,您可以在自定义单元格中找到当前的视图控制器。
您必须以这种方式添加按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[view addSubview:button];
其中aMethod是IBAction返回方法。