2013-05-27 47 views
1

C/xcode极客!PrepareForSeque未被调用/已解雇

我一直在为这个错误奋斗了好几个小时了,我似乎找不到解决方案,尽管看起来很多人都有完全相同的问题。

见代码:

// 
// 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 

正如你所看到的,我已经implented的NSLog()的prepareForSegue里面,但它不会被触发。

你们有什么想法我做错了吗?我对此很新,所以我还没有发现这整个iPhone的发展。因此,如果解决方案很简单,那么我就忍不住了:o)。

在此先感谢!

+0

您是否调用了performSegueWithIdentifier:sender:in tableView:didSelectRowAtIndexPath:? – geraldWilliam

+0

那么,你有没有触发segue?你是否通过从按钮或tableview单元格拖动到其他viewController来创建它? – Mario

+0

感谢您的快速解答。我没有任何“tableView:didSelectRowAtIndexPath”,但我也不应该。我做了很多关于这个教程:http://teamtreehouse.com/library/ios-5-foundations/storyboards/segue和他们的教程,他们也不使用它。我应该添加该代码吗?因为他们没有运行。是的。我用故事板添加连接 - 截图可能有所帮助,请参阅:http://cl.ly/image/2H071r453Z2Y – FooBar

回答

1

你有一个segue的名字,那么这是否意味着你通过从按钮或表格视图单元拖动到另一个视图控制器来创建它,然后选中它并在xcode中命名它?如果您以编程方式更改视图,则不会被调用,但您可以在交换视图之前手动调用方法以准备

我在going about sharing information in ios between views中遇到类似问题,其中答案可能有所帮助。

特别是从那里,他说对职位的答案:

“在你的.m文件,你会触发你Segue公司 - 也许上的按钮或者一些行动听起来就像你已经有这个:”

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

-

在课程交换你SEGUE名。

+0

嗨迈克!感谢您的回答aswel!我试着用这个代码解决你的问题: ' - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :(NSIndexPath *)indexPath {self performSegueWithIdentifier:@“ShowMenu”sender:self]; }' 但是,然后该应用程序崩溃。有这个理由: _ [ setValue:forUndefinedKey:]:这个类不是关键值编码兼容的关键textView。'_ – FooBar

+0

(对不起 - 我的评论是张贴错误,在我完成之前。现在编辑) – FooBar

+0

啊!但现在我意识到prepareForSeque已经运行了!到现在为止还挺好! – FooBar