-1

I am trying to push data to a UIViewController from a UITableViewController. When the user presses a climb it is going to push to the UIViewController and display 3 different specs in 3 labels which I have placed on my UIViewController. It is pushing to the detail view, but is not displaying any information. I will post my code:

FourthTableViewController.h

#import <UIKit/UIKit.h>

@interface FourthTableViewController : UITableViewController

@property (nonatomic, strong) NSString *sectionName;

@end

FourthTableViewController.m

#import "FourthTableViewController.h"
#import "DetailViewController.h"

@interface FourthTableViewController ()

@end

@implementation FourthTableViewController
{
NSArray *climbs;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//populating arrays
NSDictionary *dict = @{@"Alabama Section 1":@[@"Alabama Climb 1", @"Alabama Climb 2", @"Alabama   
Climb 3"], @"Georgia Section 1": @[@"Georgia Climb 1", @"Georgia Climb 2", @"Georgia Climb 3"], 
@"Tennessee Section 1":@[@"Tennessee Climb 1", @"Tennessee Climb 2", @"Tennessee Climb 3"], 
@"Colorado Section 1":@[@"Colorado Climb 1", @"Colorado Climb 2", @"Colorado Climb 3"]};

climbs = dict[self.sectionName];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return climbs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ClimbCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = climbs[indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:17];

return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowDetails"])
{
    DetailViewController *detailViewController = [segue destinationViewController];
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    int row = [indexPath row];
}

}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
{
IBOutlet UILabel *Type;
IBOutlet UILabel *Coordinates;
IBOutlet UILabel *Grade;
}

@property (strong, nonatomic) NSString *detailLabelContents;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;

@property (nonatomic, strong) NSString *climbName;

@end

DetailViewController.m

#import "DetailViewController.h"
#import "FourthTableViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
{
NSArray *specs;
}

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

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//populating arrays
NSDictionary *dict = @{@"Alabama Climb 1":@[@"Alabama Spec 1", @"Alabama Spec 2", @"Alabama Spec 
3"], @"Georgia Climb 1": @[@"Georgia Spec 1", @"Georgia Spec 2", @"Georgia Spec 3"], @"Tennessee 
Climb 1":@[@"Tennessee Spec 1", @"Tennessee Spec 2", @"Tennessee Spec 3"], @"Colorado Climb 
1":@[@"Colorado Spec 1", @"Colorado Spec 2", @"Colorado Spec 3"]};

specs = dict[self.climbName];

self.detailLabel.text = self.detailLabelContents;

if ([_climbName isEqualToString:@"Alabama Climb 1"])
{
    Type.text = @"Really Hard";
    Coordinates.text = @"10.32145, -83.498621";
    Grade.text = @"V10";
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

I know that my issue is in my segue, but am unsure of how to get the information from my if statement to push to the detail view to the 3 labels.

I am not looking for someone to hold my hand and tell me the answer, just some proper guidance and constructive criticism.

Thanks

javaGeek
  • 304
  • 1
  • 4
  • 11
  • Something seems wrong here - it's way way too complicated. It's possible you need to get in to making up a scene, from, containers: http://stackoverflow.com/a/23403979/294884 (note in particular right at the end how it explains how to "get at" the other view controllers). Secondly you probably need a singleton "boss". **Aside** if you have 300 (!) climbs, what you're doing is totally wrong and you're adding endless effort for yourself. the only realistic solution today is to **use parse.com and do it in the cloud**, it's incredibly easy. Failing that, use the whole sql thing inside iOS. – Fattie May 18 '14 at 07:55

1 Answers1

0

if you want to set data in your destination view controller (the controller you are pushing) you use the prepareForSegue method like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"ShowDetails"]){
        UIViewController *vc = [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        int row = [indexPath row];

        vc.climbName = ...;
        vc. detailLabelContents = ...;
    }
}

most likely you will have to cast the destinationViewController to the ViewController class you are using e.g. DetailViewController *vc = (DetailViewController*)[segue destinationViewController]; then just set what ever property you want to store your data in. With the NSIndexPath you can access your local data just like you would in cellForRowAtIndexPath

Argent
  • 390
  • 1
  • 3
  • 11
  • How do I get my 3 labels to show up though (Type, Coordinates, and Grade)?And what would i set "vc.climbName" to? – javaGeek May 18 '14 at 06:03
  • You have to create your `UILabels` in the storyboard and than connect it to the outlines of your class. Then in your viewWillAppear method you can set `Type.text` , `Grade.text`, etc to the data you provided in your string properties from the pushing view controller. What data you set there depends on what you want to display.. – Argent May 18 '14 at 06:10
  • But I have like 300+ climbs, so each climb will have different types, grades, coordinates. – javaGeek May 18 '14 at 06:13
  • that is why you set your data from the pushing view controller, where you have all the information. and you decide which information to pass to new view controller (display on detailviewcontroller) by the row the user selected `NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];` – Argent May 18 '14 at 06:16