我有一个带有UITableView * myTable和MKMapView * myMap的ViewController在xib中设计,但是表委托/数据源和地图委托在另一个名为SubClass的类中。当我在ViewController中按下一个按钮时,SubClass在xml远程文件中解析表格的纬度和经度,现在我想在每次选择myTable的行时将myMap缩放到这个坐标中:好吧,我找不到办法从SubClass调用此缩放。这是简化的,我的代码:
ViewController.h
// ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "SubClass.h"
@interface ViewController : UIViewController {
IBOutlet UITableView *myTable;
IBOutlet MKMapView *myMap;
SubClass *subClassIstance;
}
- (void)buttonPressed:(id)sender
@property (nonatomic, retain) IBOutlet MKMapView *myMap;
ViewController.m
// in ViewController.m
- (void)buttonPressed:(id)sender {
subClassIstance = [[SubClass alloc] init];
myTable.delegate = SubClass;
myTable.dataSource = SubClass;
[myTable reloadData];
subClassIstance = [[SubClass alloc] loadMap:myMap];
}
SubClass.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface SubClass : NSObject <UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate> {
}
- (void)loadValues;
- (id)loadMap:(MKMapView *)mapView;
- (id)zoomTheMap:(NSMutableString *)string1 :(NSMutableString *)string2 :(MKMapView *)mapView; // IS IT RIGHT???
SubClass.m
- (id)init{
self = [super init];
if ( self != nil ) {
[self loadValues];
}
return self;
}
- (void)loadValues {
// CODE TO PARSE VALUES OF LONGITUDE AND LATITUDE TO PASS IN THE TABLE CELLS
latitudeFromLoadValues = // NSMutableString parsed value from a xml remote file
longitudeFromLoadValues = // NSMutableStringparsed value from a xml remote file
}
- (id)loadMap:(MKMapView *)mapView
{
if (self) {
mapView.delegate = self; // CODE TO LOAD ANNOTATIONS AND OTHER STUFF. IT WORKS!
}
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
latitudeFromLoadValues = [dataParsed objectAtIndex:indexPath.row];
longitudeFromLoadValues = [data2Parsed objectAtIndex:indexPath.row];
[self zoomTheMap:latitudeFromLoadValues :longitudefromLoadValues :???]; // IS IT CORRECT? WHAT IS THE RIGHT *MKMAPVIEW?
}
- (id)zoomTheMap:(NSMutableString *)string1 :(NSMutableString *)string2 :(MKMapView *)mapView {
NSLog(@"%@",string1);
NSLog(@"%@",string2);
MKCoordinateRegion region;
region.center.latitude = [string1 floatValue];
region.center.longitude = [string2 floatValue];
region.span.latitudeDelta = 2.0;
region.span.longitudeDelta = 2.0;
// I KNOW, I HAVE TO CALL myMap from ViewController! But with an istance?
mapView.delegate = self;
mapView.region = region;
return self;
}
好吧,剩下的代码可以运行了!我可以看到ViewController中的* myMap加载了在SubClass中声明的一些注释,而* myTable加载了填充了在SubClass中解析的纬度和经度的单元格;我也可以在string1和string2中看到正确的经度和纬度,但是当我选择单个表格单元格时,我看不到myMap缩放,我认为我使用了错误的方法。你可以帮助我吗?
答案 0 :(得分:1)
loadMap
不应该返回self
,只有init方法应该这样做。
在buttonPressed
中,您分配一个新的SubClass,为它做一些事情,然后分配另一个SubClass并调用它的loadMap
函数。最后一行应该是[subClassIstance loadMap:myMap]
,但是每次按下该按钮时,您还需要重新考虑分配新的SubClass。
我认为你真的是以错误的方式解决这个问题。为什么你需要一个SubClass(可怕的名字BTW,它没有说明它的用途)?它扩展到什么课程?如果ViewController具有MKMapView,通常是向地图发出命令的那个。我可以理解你有一个单独的datasoucre for tableview,但不是其余的。如果你将VC作为自己的表并映射委托,那么你将简化很多事情。
如果你真的想在你的代码中有一个子类,那么你应该在你在buttonPressed的第一行创建的实例上调用loadMap
- (void)buttonPressed:(id)sender {
subClassIstance = [[SubClass alloc] init];
myTable.delegate = SubClass;
myTable.dataSource = SubClass;
[myTable reloadData];
[subClassIstance loadMap:myMap];
}
,你的loadMap看起来像
- (void)loadMap:(MKMapView *)mapView
{
mapView.delegate = self;
}
然而,如果这是所有loadMap你不需要一个函数,你可以让buttonPressed做到这一切。
- (void)buttonPressed:(id)sender {
subClassIstance = [[SubClass alloc] init];
myTable.delegate = SubClass;
myTable.dataSource = SubClass;
[myTable reloadData];
myMap.delegate = subClassIstance;
}
init函数示例:
- (id)initiWithMapView: (MKMapView)* mapView
{
self = [super init];
if(self)
{
theMap = mapView;
theMap.delegate = self;
[self loadValues];
....
}
return self;
}
如果你使用它,你不必一直设置map委托或返回self,你可以在每个函数中使用map(在你的答案中声明)。
答案 1 :(得分:0)
好吧,我找到了一个简单的解决方案,对于那些感兴趣的人:首先,我在我的SubClass.h中定义了一个通用的MKMapView * theMap,现在看起来像:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface SubClass : NSObject <UITableViewDataSource, UITableViewDelegate,
MKMapViewDelegate> {
MKMapView *theMap // NEW CODE!!!
}
- (void)loadValues;
- (id)loadMap:(MKMapView *)mapView;
- (id)zoomTheMap:(NSMutableString *)string1 :(NSMutableString *)string2 :(MKMapView *)mapView;
在loadMap方法中,我将* theMap与由VC中的SubClassIstance调用的mapView(我想要缩放的my * myMap)进行了比较,现在我们有:
- (id)loadMap:(MKMapView *)mapView
{
if (self) {
mapView.delegate = self;
theMap = mapView; // NEW CODE !!!
// CODE TO LOAD ANNOTATIONS AND OTHER STUFF. IT WORKS!
}
return self;
}
在didSelectRowAtIndexPath中,我已将* theMap作为mapThe参数传递给zoomTheMap方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
latitudeFromLoadValues = [dataParsed objectAtIndex:indexPath.row];
longitudeFromLoadValues = [data2Parsed objectAtIndex:indexPath.row];
[self zoomTheMap:latitudeFromLoadValues :longitudefromLoadValues :theMap]; // NEW CODE !!!
}
zoomTheMap方法不会改变,现在,“神奇地”,每次按下我的表格的一行时,在VC xib中设计的* myMap(但是在SubClass中使用委托)会缩放到存储在细胞:
- (id)zoomTheMap:(NSMutableString *)string1 :(NSMutableString *)string2 :(MKMapView *)mapView {
MKCoordinateRegion region;
region.center.latitude = [string1 floatValue];
region.center.longitude = [string2 floatValue];
region.span.latitudeDelta = 2.0;
region.span.longitudeDelta = 2.0;
mapView.delegate = self; // here mapView is *theMap passed in didSelectRowAtIndexPath, AKA original mapView istance used to delegate *myMap in VC
[mapView setRegion:region animated:YES];
return self;
}
也许它不是一种“优雅”的方式,但它现在有效! =)