Search code examples
objective-ciphonejsonuitableviewios9.1

objective-c send url according to my menu select


i have a custom UItableView . i am loading data from a API and my main view are i am loading data from a API . enter image description here

My problem is,i have a left-side menu, i want to send a url when user select any menu acoording to select menu view will load. enter image description here

-(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"

here my api link . if user select Home-menu then view will load form homePagesNews API else user select second menu then view will from jatioNews API

from this code i am getting data

-(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];

}

now i want when user select menu may home-menu or second-menu or third-menu according to user select url link will change and view will load in my ContactsTableViewController

my 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];

}

some can tell me how can i solve my problem ... Thanks


Solution

  • Add a url property to ContactsTableViewController:

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

    and in its viewDidLoad method you can load the data using whatever method you are using:

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

    Then set the URL from the tableview delegate method:

    -(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];
    }