我是iOS开发中的newBie。我为我的应用程序制作了一个分段控件,我想为每个Scrollview包含不同的WebService数据包含我使用HMSegmented Controll并设置一个scrollview就像它一样
这里我的主视图Controller Contain Code比作
ViewController *latest=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[latest.view setFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
latest.index=0;
[self addChildViewController:latest];
[self.scrollView addSubview:latest.view];
ViewController *latest2=[[ViewController alloc]initWithNibName:@"NavuViewController" bundle:nil];
[latest2.view setFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];
latest2.index=1;
[self addChildViewController:latest2];
[self.scrollView addSubview:latest2.view];
ViewController * latest3 =[[ViewController alloc]initWithNibName:@"NavuViewController" bundle:nil];
[latest3.view setFrame:CGRectMake(640, 0, self.view.frame.size.width, self.view.frame.size.height)];
latest3.index=2;
[self addChildViewController: latest3];
[self.scrollView addSubview: latest3.view];
然后它包含我想要的不同视图 在我的View Controller.h文件中,我将index定义为int Variable
@property (assign) int index;
现在我想从不同的Web服务中获取数据基于索引我在我的ViewController.m文件中编写代码ViewDidLoad方法我写了一个像
的代码-(void)view
{
[super viewWillAppear:animated];
[self.navuTable deselectRowAtIndexPath:self.navuTable.indexPathForSelectedRow animated:YES];
NSURL *url;
switch (self.index)
{
case 0:
url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/latest_post.php?page=%d",pageNum]];
break;
case 1:
{
NSString *urlString = @"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%9C%E0%AA%BE%E0%AA%A3%E0%AA%B5%E0%AA%BE%20%E0%AA%9C%E0%AB%87%E0%AA%B5%E0%AB%81%E0%AA%82&page=";
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",urlString,pageNum]];
}
break;
case 2:
{
NSString *urlString = @"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%9F%E0%AB%87%E0%AA%B2%E0%AB%87%E0%AA%A8%E0%AB%8D%E0%AA%9F&page=";
url= [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",urlString,pageNum]];
}
break;
default:
break;
}
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL: url];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
-(void)fetchedData:(NSData *)responsedata
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.navuTable.tableFooterView = spinner;
if (responsedata.length > 0)
{
NSError* error;
self.json= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
[self.navuArray addObjectsFromArray:arr];
[self.navuTable reloadData];
[spinner startAnimating];
NSLog(@"Array %@",self.navuArray);
}
}
然后是为所有索引加载相同数据意味着仅从第一个索引URL获取数据不同分段控制的不同索引的Mot加载数据请给我解决方案。 提前谢谢。
答案 0 :(得分:0)
索引始终保持为0,它不是第一项,但初始化时它是默认值。
尝试添加delegate
的{{1}},查看文档并实施Segment
:ValueChanged
,其中您可以通过{{获取所选索引的值1}},这将是您应该用来获取不同网址的值。
否则你的代码结构是这么一周,你应该使用storyboard而不是nibs,也许使用干净的代码创建一个Model类,然后连接到你的UI代码。 (MVC模式)
希望有所帮助,如果您需要更多帮助,请发布更多数据。祝你好运