iOS协议问题
我盯着代码看得太久了,我知道我在用我的协议做一些愚蠢的事情,如果有人能启发我,那就太好了 正在尝试使我的areaNameLabel在ViewController中更改为cell.nameLabel.textiOS协议问题,ios,uiviewcontroller,protocols,Ios,Uiviewcontroller,Protocols,我盯着代码看得太久了,我知道我在用我的协议做一些愚蠢的事情,如果有人能启发我,那就太好了 正在尝试使我的areaNameLabel在ViewController中更改为cell.nameLabel.text FirstTableViewController.h #import <UIKit/UIKit.h> #import "FirstTableCell.h" #import "SecondViewController.h" @interface FirstTableViewCo
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;
任何其他的批评都可以随意加入-我已经看了一些其他协议问题和示例,但我知道这是我明显缺少的。问题是您的
SecondViewController
与passNames
协议没有关系(在同一标题中声明不算在内)
由于需要实现协议方法(或者它们的实现是从基础继承的),而您的SecondViewController
不能做到这一点,因此您不能调用setAreaName:
,而不会触发错误
如果要在两个视图控制器中使用通用协议,则需要执行以下操作:
- 为
协议提供一个以大写字母开头的更常规的名称,并将其放在单独的头文件中passNames
- 在两个视图控制器中都包含该标题(第一个TableViewController.h中的导入“SecondViewController.h”看起来不正确)
- 在两个视图控制器中放置
的实现setAreaName:
请注意,您不能将通用功能放在超类中,因为视图控制器继承自不同的基础(即
UIViewController
和UITableViewController
)。问题是什么?谢谢!是的,我以前把我的协议放在一个单独的头文件中,但是发现了一个新的教程,它向我展示了如何将它保存在同一个文件中,这似乎只是让我感到困惑并导致我出错。再次感谢你,你帮我省去了很多头痛!
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!");
}
}