使用自定义单元格连接和使用表格内的按钮

时间:2013-12-07 20:12:31

标签: ios iphone objective-c

我正面临一个小问题。我有一个有2个班级的桌子。使用UIViewcontroller作为根视图,在其中我有一个孩子UITableViewCell。我定制了一个单元格,里面有按钮。

问题:如何连接和使用按钮?

我尝试了一些提到的东西和解决方法......但没有用。最后,我非常谨慎,以至于我会尝试贴上一个标签并使用它的标签使其成为按钮,如果可能的话。

1)我尝试将按钮连接到根视图,但在构建时导致了故事板错误。

2)我尝试将按钮连接到UITableViewCell。没关系,但在我想要在-(IBAction)btnclicked内使用的代码中,按钮是:

-(IBAction)btnclicked
{
        profileViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherView"];
        [self presentViewController:second animated:YES completion:nil];
} 

当我使用此代码时,我收到这些错误消息

表示方法内的第一行: 在'AnotherView'

类型的对象上找不到属性'storyboard'

第二行: 'OtherUsersCell'没有可见的@interface声明选择器'presentViewController:第二个动画:YES完成:'

我使用上面的代码连接我的观点而不是故事板segues。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我找到了!这就是我所做的,首先是这种方法:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath

如下所示,我给了我的按钮一个标签(在我的情况下为-16),并确定了一个选择器,指向按钮被粘贴时应该运行的方法,在我的情况下,方法名称为ButtonClicked

UIButton *Button= (UIButton*)[self.view viewWithTag: -16];
Button.tag = -16;
[Button addTarget:self action:@selector(ButtonClicked) forControlEvents:UIControlEventTouchUpInside];

以下是我的方法代码:

-(void) ButtonClicked
{
        buyViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"MyOtherView"];
        [self presentViewController:second animated:YES completion:nil];
}

我遇到了不同类型对象的相同问题,到目前为止,我使用的标签可以从属性检查器分配给视图中的任何对象。我跳这有助于任何面临同样问题的人。