2015-11-04 66 views
1

我有一個自定義的UItableView。我正在從API加載數據,我的主視圖是 我正在從API加載數據。 enter image description hereobjective-c根據我的菜單發送url選擇

我的問題是,我有一個左側的菜單,我想發送一個URL,當用戶選擇任何菜單acoording選擇菜單視圖將加載。 enter image description here

- (空)的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鏈接。如果用戶選擇主頁菜單然後查看將加載形式homePagesNews API別的用戶選擇第二菜單,然後從jatioNews API

從該代碼我收到數據

視圖將 - (無效)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]; 

}

現在我想當用戶選擇菜單可能主頁菜單或第二菜單或第三菜單根據用戶選擇url鏈接將改變和查看將加載我的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]; 

} 

一些能告訴我怎樣才能解決我的問題...謝謝

+0

那麼你實際上問的是如何讓網址進入'ContactsTableViewController'實例? – trojanfoe

+0

@trojanfoe是的..請再次檢查我的代碼.i更新 –

+1

添加'url'屬性到視圖控制器並設置它?在這種方法中有很多重複的代碼可以減少到10行。 – trojanfoe

回答

1

添加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 
    }]; 

然後設置在的tableview委託方法的網址:

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

它不工作..我的視圖代碼是 –

+1

@FerrakkemBhuiyan你在'viewDidLoad'方法(設置斷點)中看到了正確的URL嗎? – trojanfoe

+0

添加了我的viewDidLoad –

相關問題