【问题标题】:Creating IBOutlets and IBActions for UIButtons in a static UITableView在静态 UITableView 中为 UIButtons 创建 IBOutlets 和 IBActions
【发布时间】:2016-02-29 11:34:38
【问题描述】:

我有一个UITableViewController,完全用Interface Builder 设计。 UITableViewController 称为 DonationTableView。我正在使用Static cellsUITableView 有 6 个部分。我使用Static cells 的原因是因为可以直接从Interface Builder 中填充内容,并且每个单元格都包含:

1) 不同的尺寸

2) 带有自己文本的标签

3) 一个按钮

我还可以在Interface Builder 中轻松使用AutoLayout,以确保此DonationTableViewController 在所有设备上看起来都合适。我知道我可以使用代码,但我还是个新手,我对Interface Builder 很有信心。

我有一个名为DonationTableViewCell 的自定义UITableViewCell 类,我已将其分配给Interface Builder 中的UITableViewCell。我现在正在尝试为每个cell 中的UIButton 创建一个IBActionIBOutlet,但是随着Assistant Editor 向上,它不会让我以您的方式实际拖动创建IBAction通常会。如果我将UITableView 更改为Dynamic,它就会允许我这样做,但如上所述,我有一个完全工作的UITableViewStatic Cells,我只想在自定义UITableViewCell 类,以便我可以单击按钮并运行操作。

基本上,我希望能够将IBAction 分配给UITableViewCell 中的UIButton。我该怎么做呢?

任何想法将不胜感激。

【问题讨论】:

  • DonationTableView 类是继承自 UITableViewController 吗?您可以从 DonationTableView.h 中查看。
  • 感谢@alicanbatur - DonationTableView 继承自 UITableViewController,而 DonationTableViewCell 继承自 UITableViewCell。第一个答案已经解决了这个问题 - 感谢您伸出援手

标签: ios objective-c iphone uitableview uibutton


【解决方案1】:

将按钮插座或 IBAction 拖到您的TableviewController

【讨论】:

  • 亲爱的@Leo - 非常感谢这里的回答。如果我这样做,它会起作用,但是我基本上可以从 UIButton 的 IBAction 调用一个动作来例如呈现一个视图控制器吗?
  • 非常感谢 Leo 的帮助 - 这就像一个魅力,我无法相信它是多么简单,我试图让它变得多么复杂!
【解决方案2】:

要在Tableview 中使用UiButton,您需要遵循这两个步骤。

(1) 在 Tableview 中赋予 UIButton 标签。

Ex : 这里你的 Button 是用 uniq 标签标识的。并且还提供 UIButton 方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 cell.button.tag = indexPath.row;
 [cell.button addTarget:self action:@selector(btn_Clicked_Method:)forControlEvents:UIControlEventTouchUpInside];
}

(2) 在 UIButton 方法中

- (IBAction)btn_Clicked_Method:(id)sender {
    NSInteger tag= ((UIButton *)sender).tag
    NSLog(@"Button row %ld",(long)tag);
 }

【讨论】:

  • 非常感谢@Maulik 伸出援手——这真的很有帮助。另一个答案只是更容易实现,但非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多