【问题标题】:Adding superview method as target to UIButton of child view from superview从 superview 将 superview 方法作为目标添加到子视图的 UIButton
【发布时间】:2014-07-15 00:02:18
【问题描述】:

我正在开发一个应用程序,其中我的所有UIViewControllers 都具有UITableView 的共同结构。 所以我创建了一个包含公共结构的UITableView 子类,然后我将此视图添加到我的UIViewController

UITableView 显示正常,但我需要将 UIButton 添加到自定义 UITableViewCell 从我的 ViewController 中添加目标,并且方法是用 UIViewController 编写的。

谁能告诉我如何做到这一点?

【问题讨论】:

  • 您使用的是 storyboard 还是旧系统?如果是故事板,这可能会有很大帮助stackoverflow.com/a/23060746/294884
  • @JoeBlow:是的,我正在使用 Storyboard
  • @JoeBlow 我尝试使用容器视图控制器来做到这一点,但这不能按照我的要求工作,因为UIButton 点击我的桌子高度正在改变
  • @ShitalTiwari :请按照我的回答中所述选择NSNotificationCenter。有一个示例代码链接解释了NSNotificationCenter 在自定义子类中的用法。

标签: ios objective-c uitableview


【解决方案1】:

您需要进行程序设计才能解决此问题。但是,您已经走得足够远,可以返回并更改您的设计。所以对你来说更好的选择是NSNotificationCenter

对于您的情况,请执行以下操作:

  1. 在编写方法的 viewController 中,假设方法名称为 myX-Method:(执行按钮的目标功能),然后在该控制器的 viewDidLoad 中添加这一行。

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myX-Method:) name:@"NOTIFY_CUSTOM_BUTTON" object:nil];
    }
    
  2. 现在,在您的 UITableViewCell 类中,像往常一样将本地方法设置为按钮目标,比如说 myLocalMethod:

  3. 现在在这个本地方法中,像这样发布通知消息。例如,我只是将当前单元格的按钮文本作为消息传递给通知。

    -(void) myLocalMethod:(UIButton *)button{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFY_CUSTOM_BUTTON" object: button.title];
    }
    

这样你就不必依赖委托了。使用通知对象在myX-Method: 中收集您的值,如下所示:

-(void)myX-Method:(NSNotification *)dict {
    NSString *buttonTitle = [dict valueForKey:@"object"];
    NSLog(@"Button Clicked : %@", buttonTitle);
}

希望这个简单的解决方案能解决您的目的。

请参阅here 以获取示例代码。但是在示例代码项目中,请搜索NSNotificationCenter

【讨论】:

    【解决方案2】:

    您可以在 tableView:cellForRowAtIndexPath: 处将单元格委托设置为视图控制器:

    然后当在单元格内按下按钮时,您可以在单元格中触发 buttonPressed: 方法内的委托方法。

    所以 cell 调用 cell.delegate 这是 viewController

    在 CellView.h 中

    @property (nonatomic, weak) id delegate;
    

    在 CellView.m 中

    - (void)buttonPressed:(id)sender {
        [self.delegate cellButttonPressed:sender];
    }
    

    在 MyTableViewController.m 中

    - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        CellView *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.delegate = self;
        return cell;
    }
    
    - (void)cellButttonPressed:(id)sender {
    //button action code here
    }
    

    【讨论】:

    • 你能详细说明一下吗?
    【解决方案3】:

    以委托方法为例

    创建 UITableViewCell 的子类

    声明委托方法

      @protocol  CustomCellDelegate <NSObject>
    
    -(void)btnClikced;
    
    @end
    

    // 接口

    @interface CustomCell : UITableViewCell
    
    @property(nonatomic,retain) id<CustomCellDelegate> delegate;
    

    //实现

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
            [btn addTarget:self action:@selector(btnTapped) forControlEvents:UIControlEventTouchUpInside];
            self.accessoryView=btn;
        }
        return self;
    }
    -(void)btnTapped{
        [self.delegate btnClikced];
    }
    

    在您的视图控制器中

    - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.delegate = self;
    return cell;
    

    }

    - (void)btnClikced{
    //button action code here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多