2014-02-09 75 views
1

我一直在盯着代码太久,知道我在这里用我的协议做一些愚蠢的事,如果有人能够启发我,这将是伟大的。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!"); 
    } 
    } 

其他任何批评随意的扔 - 我有看一些其他的协议问题,并例子,但我知道这是显而易见的,我失踪了。

+2

问题是什么? – ahmedalkaff

回答

0

问题是您的SecondViewControllerpassNames协议没有关系(在同一个头文件中声明的不计数)。

由于协议方法需要实现(或者它们的实现需要从基地继承)并且您的SecondViewController不这样做,所以您不能在不触发错误的情况下调用setAreaName:

如果你想两个视图控制器使用共同的协议,你需要做的是:

  • passNames协议,开始在一个大写字母一个更方便的名字,并把它放在一个单独的头文件
  • 包括在这两个视图控制器,报头(在该FirstTableViewController.h#import "SecondViewController.h"不右看)在这两个视图控制器的setAreaName:
  • 穿戴实现。

注意,你不能把常用功能的一个超类,因为您的视图控制器从不同的基础(即UIViewControllerUITableViewController)继承。

+0

谢谢!是的,我以前把我的协议放在一个单独的头文件中,但找到了一个新的教程,告诉我如何将它保存在同一个文件中,这似乎只是让我迷惑并导致我犯错误。所以再次感谢,你为我节省了很多头痛! – jkd359