objective-c根据我的菜单发送urlselect

objective-c send url according to my menu select

我有一个自定义的 UItableView 。我正在从 API 加载数据,我的主视图是 我正在从 API 加载数据。

我的问题是,我有一个左侧菜单,我想在用户 select 将加载符合 select 菜单视图的任何菜单时发送 url。

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

if(indexPath.row ==0){
    appdataModel.newsApiUrl = homePagesUrl;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];

    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];

}else if (indexPath.row ==1){
    appdataModel.newsApiUrl = jatioNews;

    NSLog(@"here  1 :%@",appdataModel.newsApiUrl);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];
    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];
}
else if (indexPath.row ==2){
    appdataModel.newsApiUrl = jatioNews;

    NSLog(@"here  1 :%@",appdataModel.newsApiUrl);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];
        ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];
}

}

#define homePagesNews @"http://198.72.115.125/~pratidin/api/topnews"
#define jatioNews @"http://198.72.115.125/~pratidin/api/categorynews/4"

这里是我的 api link 。如果用户 select 主菜单然后视图将加载形式 homePagesNews API 否则用户 select 第二个菜单然后视图将从 jatioNews API

我从这段代码中获取数据

-(void)GetHomePageData{

NSString *urlString   = [NSString stringWithFormat:@"%@",url];
NSURL *url            = [[NSURL alloc]initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSData *GETReply      = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];

}

现在我希望用户 select 菜单可以根据用户 select url link 的主菜单或第二菜单或第三菜单更改和查看将加载到我的 ContactsTableViewController

我的 ContactsTableViewController viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    appdataModel = [AppDataModel getInstance]; 
    appdataModel.newsApiUrl = homePagesUrl;

     /**** for left side menu ***/

    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.sideBarButton setTarget: self.revealViewController];
        [self.sideBarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }

     /**** for Contractview***/
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
    contactsArray  = [NSArray arrayWithContentsOfFile :path];
    [self GetHomePageData];

    [self.newsDataTableView reloadData];

}

有人可以告诉我如何解决我的问题...谢谢

添加一个url 属性到ContactsTableViewController:

@interface ContactsTableViewController : UIViewController
@property NSURL *url;
...
@end

并且在其 viewDidLoad 方法中,您可以使用您正在使用的任何方法加载数据:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
  dataTaskWithURL:self.url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // Whatever
    }];

然后从表视图委托方法中设置URL:

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

    if (indexPath.row ==0){
        appdataModel.newsApiUrl = homePagesUrl;
    } else {
        appdataModel.newsApiUrl = jatioNews;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];

    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    vc.url = [NSURL URLWithString:appdataModel.newsApiUrl];
    [self.navigationController pushViewController:vc animated:YES];
}