【问题标题】:Objective-C: How can I pass string to a .xib file and display a MapView?Objective-C:如何将字符串传递给 .xib 文件并显示 MapView?
【发布时间】:2014-05-08 19:59:37
【问题描述】:

我有一个 .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 方法,因为它现在仅在从类中搜索位置时显示正确的位置,并且在实例化时不显示正确的位置。

【问题讨论】:

  • Xib 文件不会“做”事情。带有 xib 扩展名的 Nib 文件只是对视图及其子视图的布局和参数的描述。相反。您的 locView 初始化时根本没有任何 nib 文件。您没有创建初始化“文件”,但您已经覆盖了 inti... 方法。虽然这听起来像是指手画脚,但准确的语言有助于避免误解,这对我们所有人都有帮助。
  • 对不起,我的意思是 init 方法不好,将编辑它。
  • 你调试过,发现文本地址的初始值是多少? textLoc.text 有什么价值,因此 _loc?
  • 哦,是的,我调试了它,它显示字符串很好,所以没什么。

标签: ios objective-c mkmapview


【解决方案1】:

在这里,您使用地址字段的内容覆盖 stringLoc1: 无论其初始值如何,它都会被覆盖。如果 textField 为空,则它被空字符串覆盖。

stringLoc1 = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

我不确定在剩余代码的上下文中最佳解决方案是什么。但这应该可行:

NSString *currentLoc;

if (addressField.text && ([addressField.text length] == 0)) {
  currentLoc = addressFiled.text;
  stringLoc1 = addressField.text; // this line will set the current value as default value for the next call of the method; unless a new init call comes in between.
}
else
  currentLoc = stringLoc1; 


    currentLoc = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [currentLoc  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:currentLoc ]];

我相信有更聪明的方法可以做到这一点。但这应该对您与我们共享的代码视图行进行最小的更改。

【讨论】:

  • 这是一个很好的解决方案,我刚刚尝试过,但仍然无法正常工作。你能给我任何指示我可能会出错的地方吗?就像我是否应该检查 viewDidLoad 方法一样?等
  • 另外我只是尝试使用该方法: - (void)viewWillAppear:(BOOL)animated 这里我尝试调用 showAddress 方法,但不幸的是结果相同。
  • viewWillAppear 应该没问题。因为它是关于一些初始值,所以只有 viewDidLoad 也应该没问题。当然,当用户更改值本身时,对按下的按钮的任何响应。这次究竟是什么不起作用?您观察到什么以及 stringLoc1 和 addressField.text 有哪些值?
  • 哦,是的 addressField 在这种情况下返回空指针。尝试提出为什么它为空并尝试修复它,如果可以的话,我稍后会在这里寻求进一步的帮助。
  • 顺便说一句,这应该不是问题,因为 addressField.text 不应该有任何值开始,因为我们刚到这个类。我调试过,城市名称正确存储在 currentLoc 和 stringLoc1 中。所以我想知道为什么它在地图上找不到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多