【发布时间】:2014-12-29 03:19:31
【问题描述】:
有人能解释一下如何将属性放入协议中以便跨不同视图控制器访问它的值的基础知识吗?
具体来说,我正在尝试将 MKMapView 属性放入协议中。我有两个视图控制器:
- Map View Controller(里面是2.Table List View Controller)
- 表格列表视图控制器
为了能够在表列表视图控制器中选择与地图视图控制器中的注释相对应的帖子,我需要能够访问地图视图控制器中的地图视图。这就是我认为协议会派上用场的地方。
地图视图控制器.h
@protocol MapViewDelegate <MKMapViewDelegate>
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@end
@interface PAWWallViewController : UIViewController <MapViewDelegate, ...>
...
@end
地图视图控制器.h
...
@synthesize mapView = _mapView
...
表格视图控制器.h
@interface PAWWallPostsTableViewController : PFQueryTableViewController <MapViewDelegate...>
...
表格视图控制器。米
...
@synthesize mapView;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// call super because we're a custom subclass.
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
PAWPost *post = [[PAWPost alloc] initWithPFObject:object];
[mapView setCenterCoordinate: post.coordinate animated:YES];
[mapView selectAnnotation:post animated:YES];
NSLog(@"MAP VIEW: %@", mapView); // STILL NULL
}
【问题讨论】:
-
请附上您看到的错误的全文。
-
协议不会神奇地使属性可用,它只是让引用更容易,而无需使用其他包含。要引用一个属性,您需要一个指向包含该属性的对象的指针。
-
我更新了。在两个 .m 文件中合成后,它现在不会发送错误。但是 self.mapView 在日志中返回 null。我添加了合成,因为我有警告说自动属性合成不会合成属性 mapView。在我添加它之前,它发送了错误:*** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[
setValue:forUndefinedKey:]:此类不符合键值编码的键映射视图。 ' *** -
@HotLicks 关于如何指向包含该属性的对象的示例?
-
首先确定它是哪个对象,然后复制指向该对象的指针。
标签: ios objective-c properties mapkit protocols