Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将superview方法作为目标添加到superview子视图的UIButton_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 将superview方法作为目标添加到superview子视图的UIButton

Ios 将superview方法作为目标添加到superview子视图的UIButton,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在开发一个应用程序,在该应用程序中,我的所有UIViewControllers都具有通用的UITableView结构。 因此,我创建了一个包含公共结构的UITableView子类,然后将该视图添加到我的UIViewController中 UITableView显示正确,但我需要将目标添加到UIButton从我的ViewController添加到自定义UITableViewCell,方法在UIViewController中写入 有人能告诉我如何实现这一点吗?您可以在tableView:cell

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

UITableView
显示正确,但我需要将目标添加到
UIButton
从我的
ViewController
添加到自定义
UITableViewCell
,方法在
UIViewController
中写入


有人能告诉我如何实现这一点吗?

您可以在tableView:cellForRowAtIndexPath:

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

因此,单元格调用作为viewController的cell.delegate

- (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;
在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
}

您需要进行程序设计,以解决此问题。然而,你已经走得够远了,可以回去改变你的设计。因此,对您来说,更好的选择是
NSNotificationCenter

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

  • 在编写方法的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];
    }
    
  • 现在,在您的
    UITableViewCell
    类中,像通常一样将一个本地方法设置为按钮目标,比如说,
    myLocalMethod:

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

    -(void) myLocalMethod:(UIButton *)button{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFY_CUSTOM_BUTTON" object: button.title];
    }
    
  • 这样,您就不必依赖于委托。在
    myX方法:
    中使用通知对象收集您的值,如下所示:

    -(void)myX-Method:(NSNotification *)dict {
        NSString *buttonTitle = [dict valueForKey:@"object"];
        NSLog(@"Button Clicked : %@", buttonTitle);
    }
    
    希望这个简单的解决方案能解决你的问题


    请参阅示例代码。然而,在示例代码项目中,请搜索
    NSNotificationCenter

    例如使用委托方法

    生成UITableViewCell的子类

    声明委托方法

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

    - (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;
    
    }


    您使用的是故事板还是旧系统?如果是情节提要,这可能会有很大帮助@JoeBlow:是的,我正在使用Storyboard@JoeBlow我尝试使用container view controller来完成此操作,但这不符合我的要求,因为在
    ui按钮上单击“我的桌子高度为”changing@ShitalTiwari:请按照我的回答选择通知中心。这里有一个示例代码链接,解释了
    NSNotificationCenter
    在自定义子类中的用法。您能详细说明一下吗?
    - (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
    }