我有一个 .xib 文件,用户可以在其中输入 UITextField 中某个地点的位置名称,然后单击搜索。然后将加载另一个 .xib 文件,用户将在其中看到带有地图位置的 MapView 对象。
我制作了一个自定义的 init 方法,当用户单击按钮时,我按如下方式传递字符串:
AddLocViewController *locView = [[AddLocViewController alloc] initWithNibName:nil bundle:nil customLoc:textLoc.text];
[self presentModalViewController:locView animated:YES];
在名为 AddLocViewController.m 的新 ViewController 类中,init 方法和 viewDidLoad 如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil customLoc:(NSString*)_loc
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
stringLoc1 = _loc;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self showAddress];
}
这里 [self showAddress] 调用一个方法来显示 mapView 对象上的内容,如果我有一个 UITextField 接受输入并单击一个按钮将值设置为 NSString 对象 stringLoc 并在 showAddress 方法中使用它,那么 showAddress 方法可以完美地工作:
- (void) showAddress
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01f;
span.longitudeDelta=0.01f;
stringLoc1 = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:stringLoc1]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double aLatitude = 0.0;
double aLongitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
aLatitude = [[listItems objectAtIndex:2] doubleValue];
aLongitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
[addressField setText: @"Address Location error"];
return;
}
[addressField resignFirstResponder];
CLLocationCoordinate2D location = {latitude: aLatitude, longitude:aLongitude};
region.span=span;
region.center.latitude = aLatitude;
region.center.longitude = aLongitude;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
//[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation: addAnnotation];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}
请帮助我将此处的参数传递给此类并适当地使用 showAddress 方法,因为它现在仅在从类中搜索位置时显示正确的位置,并且在实例化时不显示正确的位置。