1

C/xcode geeks!

I have been struggling with this error for hours now, and I don't seem to find the solution, although it seems as many people have had the exact same problem.

See code:

//
//  ListViewController.m
//  Puns
//
//  Created by Amit Bijlani on 12/13/11.
//  Copyright (c) 2011 Treehouse Island Inc. All rights reserved.
//

#import "ListViewController.h"
#import "BlogPost.h"
//#import "Pun.h"
#import "PunsTableViewCell.h"
#import "DetailViewController.h"

@implementation ListViewController

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

#pragma mark - Segue

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"0");
    if ( [[segue identifier] isEqualToString:@"ShowMenu"] ){
        NSLog(@"1");
        DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
        dvc.menu = [self.blogPosts objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
    }
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    [super viewDidLoad];

    // GET WEB DATA SOURCE (JSON)

    // The url
    NSURL *blogURL = [NSURL URLWithString:@"http://constantsolutions.dk/json.html"];

    // The data
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    // Error variable
    NSError *error = nil;

    // jsonData to serialization
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    // Get array 'posts'
    self.blogPosts = [NSMutableArray array];
    NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"];

    for (NSDictionary *bpDictionary in blogPostsArray) {
        // Get title
        // Will only retrieve data, if TITLE exists

        BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];

        // Get the content
        blogPost.content = [bpDictionary objectForKey:@"content"];

        // Get thumbnail
        blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];

        // Get date
        blogPost.date = [bpDictionary objectForKey:@"date"];

        // Get price
        blogPost.price = [bpDictionary objectForKey:@"price"];

        // Add the object to blogPosts array
        [self.blogPosts addObject:blogPost];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.blogPosts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PunTableViewCell";

    PunsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PunsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    cell.menuTitle.text = blogPost.title;
    cell.menuContent.text = blogPost.content;

    if([blogPost.price length] == 0) {
        [cell.menuPrice setHidden:YES];
    } else {
        cell.menuPrice.text = blogPost.price;
    }



    return cell;
}


@end

As you can see, I have implented NSLog() inside the prepareForSegue, but it is not triggered.

Do you guys have any idea what I am doing wrong? I am pretty new to this, so I still haven't found out this whole iPhone development thing. So bare over with me if the solution is simple :o).

Thanks in advance!

FooBar
  • 5,752
  • 10
  • 44
  • 93
  • Did you call performSegueWithIdentifier:sender: in tableView:didSelectRowAtIndexPath:? – geraldWilliam May 27 '13 at 18:14
  • Well, do you have a trigger for the segue? Did you create it by dragging from a button or tableview cell to your other viewController? – Mario May 27 '13 at 18:14
  • Thanks for your quick answers. I don't have any "tableView:didSelectRowAtIndexPath" but nor was I supposed to. I did pretty much follow this tutorial: http://teamtreehouse.com/library/ios-5-foundations/storyboards/segue and in their tutorial they dont use it either. Am I supposed to add that code? Because their run without. And yes. I used storyboard to add the connection - screenshot might help, see: http://cl.ly/image/2H071r453Z2Y – FooBar May 27 '13 at 20:12

1 Answers1

1

You have a name for the segue, so does that mean you created it by dragging from a button or table view cell to the other view controller and then selected it and named it in xcode? if you are programmatically changing the view it wont be called but you can manually call a method before swapping views to prepare

I had a similar issue in going about sharing information in ios between views where the answer may be helpful.

In particular from the answer on that post where he says:

"in your .m file you would trigger you segue - maybe on a button or some action. sounds like you already have this:"

[self performSegueWithIdentifier:@"viewB" sender:self];

--

Swapping your segue name in of course.

Community
  • 1
  • 1
LanternMike
  • 664
  • 1
  • 5
  • 16
  • Hi Mike! Thanks for your answer aswel! I have tried your solution with this code: `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"ShowMenu" sender:self]; }` But then the app crashes. With this reason: _[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key textView.'_ – FooBar May 27 '13 at 20:17
  • (sorry - my comment was posted by mistake, before I was finished. Comment now edited) – FooBar May 27 '13 at 20:20
  • Ah! But now I realize that the prepareForSeque is runned! So far so good! – FooBar May 27 '13 at 20:23
  • from what i'm seeing in the eror i might look for things refered to on the storyboard not named, maybe a textView. – LanternMike May 27 '13 at 20:30
  • in http://stackoverflow.com/questions/11135047/use-prepareforsegue-for-button it says self is the view controller. I'd check into if self needs to be a viewcontroller. in your tableview self at that scope might be a table view but i'm honestly not certain not having implemented a tableview that way. – LanternMike May 27 '13 at 20:40
  • Thanks for the link! I have checked out, but it doesn't seem to solve my problem. I can conclude that the app crashes on this line: `dvc.menu = [self.blogPosts objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];` The dvc.menu value is: ****. I really got no clue, I have followed every instruction I could find. If it helps, I have uploaded the: [BlogPost.m](http://codeviewer.org/view/code:33b9), [BlogPost.h](http://codeviewer.org/view/code:33ba) and [DetailViewController.h](http://codeviewer.org/view/code:33bb) for you to look at. – FooBar May 27 '13 at 20:45
  • What do you mean excatly? I use the variable menu here: [DetailViewController.m](http://codeviewer.org/view/code:33bc) – FooBar May 27 '13 at 20:56
  • I have declared it like this in BlogPost.h: @property (strong, nonatomic) BlogPost *menu; (don't be mistaken by the name BlogPost, it is actually just a fetch from an JSON element, fetching "todays menus". I have just copied that from another script I made. "Great artists steal" they say.... :)) – FooBar May 27 '13 at 21:03
  • how was menu declared in the .h? do you have objects at the indexes i.e. you've initialized your cells to something? is something selected? It is sort] of sounding like the view you want to load has a table view and your setup of the table view might be failing or some part of that view is failing but it is loading. – LanternMike May 27 '13 at 21:04