Question

I have been staring at code too long and know i am doing something silly here with my Protocols if someone could enlighten me that would be great.

Trying to get my areaNameLabel to change to cell.nameLabel.text across viewcontrollers.

FirstTableViewController.h

#import <UIKit/UIKit.h>
#import "FirstTableCell.h"
#import "SecondViewController.h"

@interface FirstTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, passNames>

@property (nonatomic, strong) NSString *passedNameString;

@property (strong, nonatomic) NSMutableArray *names;

FirstTableViewController.m

#import "FirstTableViewController.h"

@interface FirstTableViewController ()

@end

@implementation FirstTableViewController
@synthesize names;
@synthesize passedNameString;


- (void)viewDidLoad
{
    [super viewDidLoad];

    names = [NSMutableArray arrayWithObjects:@"Bondi", @"Miranda", nil];

}

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

    FirstTableCell *cell = (FirstTableCell *)[tableView cellForRowAtIndexPath:indexPath];

    if ([cell.nameLabel.text isEqualToString:@"Bondi"]) {

        SecondViewController *mapController = [[SecondViewController alloc] init];

        NSString *passedName = cell.nameLabel.text;

        mapController.passedNameString = passedName;

        [mapController setDelegate:self];

        self.tabBarController.selectedIndex = 1;
        NSLog(@"Hola");



    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Protocol Methods

-(void)setAreaName:(NSString *)areaName {

    passedNameString = areaName;
}

SecondViewController.h

#import <UIKit/UIKit.h>

@protocol passNames <NSObject>

-(void)setAreaName:(NSString *)areaName;

@end

@interface SecondViewController : UIViewController <RMMapViewDelegate>

@property (retain) id <passNames> delegate;

@property (nonatomic, strong) NSString *passedNameString;

@property (weak, nonatomic) IBOutlet RMMapView *mapView;
@property (weak, nonatomic) IBOutlet UILabel *areaNameLabel;



@end

SecondViewController.m

#import "SecondViewController.h"
#import "FirstTableViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController @synthesize areaNameLabel; @synthesize delegate, passedNameString;


- (void)viewDidLoad {
    [super viewDidLoad];    

    passedNameString = areaNameLabel.text;
    [[self delegate] setAreaName:passedNameString];

    if ([areaNameLabel.text isEqualToString:@"Bondi"]) {

        NSLog(@"You got it!");
    }
     }

Any other critiques feel free to throw in - I've had a look at some other Protocol questions and examples but i know it is something obvious i am missing.

Was it helpful?

Solution

The problem is that your SecondViewController has no relationship to the passNames protocol (being declared in the same header does not count).

Since protocol methods need to be implemented (or their implementation be inherited from the base) and your SecondViewController does not do that, you cannot call setAreaName: without triggering an error.

If you would like to use a common protocol in two view controllers, you need to do this:

  • Give passNames protocol a more conventional name that starts in a capital letter, and put it in a separate header file
  • Include that header in both view controllers (the #import "SecondViewController.h" in the FirstTableViewController.h does not look right)
  • Put implementations of setAreaName: in both view controllers.

Note that you cannot put the common functionality in a superclass, because your view controllers inherit from different bases (i.e. UIViewController and UITableViewController).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top