我一直在盯着代码太久,知道我在这里用我的协议做一些愚蠢的事,如果有人能够启发我,这将是伟大的。iOS协议问题
试图让我的areaNameLabel跨ViewController更改为cell.nameLabel.text。
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!");
}
}
其他任何批评随意的扔 - 我有看一些其他的协议问题,并例子,但我知道这是显而易见的,我失踪了。
问题是什么? – ahmedalkaff