Lovely Lovely - 2 months ago 8
iOS Question

Custom delegates to get the data displayed on tableview cells

I have a button on

ViewController A
and
tableview
with custom cells on
ViewController B
. I want to display the data selected on cells of
ViewController B
as the currentTile of
button
on
ViewController A
. I know this can be achieved through "delegates" but I am not able to code for it. I will be very grateful if someone tells me the code for it as I am very new to iOS Development.

Thanks in advance!

Answer

Use this code:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<SecondViewControllerPotocol>
@property (weak, nonatomic) IBOutlet UIButton *sampleButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)navigateToSecondVCOnButtonClick:(id)sender {

    SecondViewController *second = (SecondViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
    second.delegate = self;
    [self.navigationController presentViewController:second animated:YES completion:nil];
}

- (void)changeButtonTitleWithText:(NSString *)title{

    [self.sampleButton setTitle:title forState:UIControlStateNormal];
}


@end

SecondViewController.h

 #import <UIKit/UIKit.h>

    @protocol SecondViewControllerPotocol <NSObject>

    @optional
    - (void)changeButtonTitleWithText:(NSString *)title;

    @end


    @interface SecondViewController : UIViewController
    //delegate
    @property (nonatomic, assign) id<SecondViewControllerPotocol> delegate;
    @end

SecondViewController.m
----

#import "SecondViewController.h"

@interface SecondViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *colorsTableView;
@property (strong, nonatomic) NSArray *colorsArray;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.colorsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.colorsArray = @[@"Red",@"Blue",@"Green",@"Yellow",@"Pink"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.colorsArray.count;
}

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

    UITableViewCell *customCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    customCell.textLabel.text = self.colorsArray[indexPath.row];   

    return customCell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *selectedCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    [_delegate changeButtonTitleWithText:selectedCell.textLabel.text];
    [self dismissViewControllerAnimated:YES completion:nil];

}


@end

Storyboard screenshot:

enter image description here

Please check my GitHub link to test sample project(Changing one button title):

https://github.com/k-sathireddy/CustomDelegateSample

For changing two button titles, please follow this:

https://github.com/k-sathireddy/CustomDelegateSampleTwo

Comments