Fxbaez
Fxbaez

Reputation: 193

Calling a View from a UItableViewCell

I am trying to make my very first app. I have 6 files:

LeftPanelViewController.h LeftPanelViewController.m LeftPanelViewController.xib ForecastViewController.h ForecastViewController.m ForecastViewController.xib

Inside LeftPanelViewController.xib i have a view that contains a table view. i want to set a cell from the table view that i have in LeftPanelViewController.xib so when i press that cell i'll be able to call the view that i have inside the ForecastViewController.xib.

below the code the i have in LeftPanelTableViewController.m

#import "LeftPanelViewController.h"
#import "ForecastViewController.h"


@interface LeftPanelViewController ()

@property (nonatomic, weak) IBOutlet UITableView *myTableView;
@property (nonatomic, weak) IBOutlet UITableViewCell *cellMain;
@property (copy, nonatomic) NSArray *MRP;

@end

@implementation LeftPanelViewController

- (void)viewDidLoad
{
[super viewDidLoad];

Array = [[ NSMutableArray alloc] init];
[Array addObject:@"Forecast"];
[Array addObject:@"Inventory"];

}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier];

//add an image to the tablecell
UIImage *image = [UIImage imageNamed:@"Icon.png"];
cell.imageView.image = image;

}
cell.textLabel.text = [Array objectAtIndex:indexPath.row];

if (indexPath.row < 7) {
cell.detailTextLabel.text = @"Account";
} else {
cell.detailTextLabel.text = @"Settings";
}
return cell;
}

- (void)viewDidUnload
{
[super viewDidUnload];
}

#pragma mark -
#pragma mark View Will/Did Appear

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

#pragma mark -
#pragma mark View Will/Did Disappear

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

#pragma mark -
#pragma mark Array Setup


#pragma mark -
#pragma mark UITableView Datasource/Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}


#pragma mark -
#pragma mark Default System Code

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

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

if ([[Array objectAtIndex:indexPath.row] isEqual:@"Forecast"]) {

ForecastViewController *Forecast = [[ ForecastViewController alloc] initWithNibName:@"ForecastViewController" bundle:nil];
[self.navigationController pushViewController:Forecast animated:YES];

}


[tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end

Upvotes: 2

Views: 261

Answers (2)

Timur Kuchkarov
Timur Kuchkarov

Reputation: 1155

Even if modal controller did the trick, it may be not what you want. Nothing was happening because your view controller is nil. Probably you are using "Single view app" template from xCode and it has no navigation controller. If you want one, you should create it like [[UINavigationController alloc] initWithRootViewController:] and set your's window root controller to newly created navigationcontroller.

Upvotes: 1

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Instead of

[self.navigationController pushViewController:Forecast animated:YES];

use

[self presentViewController:Forecast animated:YES completion:nil];

Upvotes: 1

Related Questions