【问题标题】:iphone SDK :How to add more string after an static URL?iphone SDK:如何在静态 URL 后添加更多字符串?
【发布时间】:2010-11-15 17:23:47
【问题描述】:

首先...如果没有人能回答这个问题也没关系...

因为我不知道如何在正确的描述中提出这个问题

我的程序是从 URL 获取 plist 数据并将其显示在 iphone tableview 上

我的同事给了我两个获取 plist 数据的 URL

所以我有两个 tableviews(tableview1,tableview2)

两个网址(URL1,URL2)

我的tableview1通过URL1得到一个plist1,tableview1前3行有3个内容 p>

例如(row0 id1),(row1 id2),(row2 id3)

当我选择 row0 时,它将跳转到 tableview2 并在导航栏上显示标题 id1

目前为止我能做到...

但是在 tableview2 中,它需要一个像这样的 URL:www.URL2.php?id=1 =>这意味着...

我必须在 URL2 之后添加我的 tableview 标题

获取与 id 1 相关的正确 plist

我的问题是.......URL2 是静态的,如何让它像动态一样获得正确的 URL 地址???

嗯.....第一次我以为我的同事会给我一个像数组数组这样的数据

但这不是...我从不尝试使用动态 URL...

这是 tableview1 中的代码,可让我从 URL 获取 plist

 NSURLRequest *theRequest=[NSURLRequest requestWithURL:appDelegate.URL1//I declare the  URL at appDelegate.m for some reasons...
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];
      NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest
                           returningResponse:nil error:nil]; 
      NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
              self.array = [listFile propertyList];//I parse the data from array to tablecview

这就是我如何将标题从 Tableview1 解析为 Tableview2

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    TableView2 *tb2 = [[TableView2 alloc]init];
    tb2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    tb2.title = [[[array objectAtIndex:indexPath.row] valueForKey:@"name"]description];
    [self.navigationController pushViewController:tb2 animated:YES];   }

有什么想法吗?

嗯......如果有人能回答这个问题或给我一个方向,我将非常感激

非常感谢所有回复和答复!!!

哦...还有一件事,我可以使用 ASIHTTPRequest 来发布数据

但我不知道如何在发布后获取数据...?(这有帮助吗?)

【问题讨论】:

    标签: iphone objective-c plist nsurl


    【解决方案1】:

    假设您有一个要附加到 appDelegate.URL1 的字符串,声明为:

    NSString *stringToAppend = @"whatever";
    

    然后你可以生成一个包含新 URL 的字符串:

    NSString *newURLAsString = [NSString stringWithFormat:@"%@%@",[appDelegate.URL1 absoluteString],stringToAppend];
    

    还有一个来自这个字符串的 NSURL:

    NSURL *newURL = [NSURL URLWithString:newURLAsString];
    

    还有来自那个的 NSURLRequest:

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:newURL
                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];
    

    【讨论】:

      【解决方案2】:

      我不确定您要说什么。

      您想从 www.foo.com 加载 tableView1,然后当用户单击一行时,它会指向另一个 URL,例如 www.foo.com/row0。

      最简单的实现方法是创建一个您在 selectedRow 方法中设置的属性

      类似的东西。

      在标题中声明属性

      @Property (readwrite,copy) NSString* strURL;
      

      【讨论】:

        【解决方案3】:

        将字符串附加到 api

        当您想将一些字符串作为参数传递给您的 Api 时 例如我存储用户的经纬度,然后将该字符串传递给 API 的 url

        NSURL *address1=[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",location]];
        
        NSLog(@"your lat and long is::%@",[NSString stringWithFormat:@"http://api.wunderground.com/api/90caafb557ba6bf7/geolookup/conditions/forecast/q/%@.json",location]);
        

        这里的 Location 是一个 NSString,它存储纬度和经度,我将该数据传递给 API 以获取城市、国家和温度等。

        在帮助下%@ 我们可以将存储的值传递给 api

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-04
          • 1970-01-01
          相关资源
          最近更新 更多