【问题标题】:PrepareForSeque not called/firedPrepareForSegue 未被调用/被解雇
【发布时间】:2013-05-22 14:55:13
【问题描述】:

C/xcode 极客!

我已经为这个错误苦苦挣扎了好几个小时,但我似乎没有找到解决方案,尽管似乎很多人都遇到了完全相同的问题。

见代码:

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

如您所见,我在 prepareForSegue 中实现了 NSLog(),但它没有被触发。

你们知道我做错了什么吗?我对此很陌生,所以我还没有发现整个 iPhone 开发的事情。如果解决方案很简单,那就跟我一起吧:o)。

提前致谢!

【问题讨论】:

  • 你在 tableView:didSelectRowAtIndexPath: 中调用 performSegueWithIdentifier:sender: 了吗?
  • 好吧,你有触发segue的方法吗?您是否通过从按钮或表格视图单元格拖动到其他视图控制器来创建它?
  • 感谢您的快速回答。我没有任何“tableView:didSelectRowAtIndexPath”,但我也不应该有。我确实非常关注本教程:teamtreehouse.com/library/ios-5-foundations/storyboards/segue 在他们的教程中他们也没有使用它。我应该添加该代码吗?因为他们跑无。是的。我使用故事板添加连接 - 屏幕截图可能会有所帮助,请参阅:cl.ly/image/2H071r453Z2Y

标签: ios objective-c xcode


【解决方案1】:

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

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

特别是从他说的那个帖子的答案中:

“在你的 .m 文件中,你会触发你的 segue - 可能是在一个按钮或一些动作上。听起来你已经有了这个:”

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

--

当然要交换你的 segue 名称。

【讨论】:

  • 嗨迈克!谢谢你的回答!我已经使用以下代码尝试了您的解决方案:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"ShowMenu" sender:self]; } 但随后应用程序崩溃了。由于这个原因:[ setValue:forUndefinedKey:]: 这个类对于键 textView 不符合键值编码。'
  • (抱歉 - 我的评论在我完成之前被错误地发布了。现在已编辑评论)
  • 啊!但现在我意识到prepareForSeque 已经运行了!到目前为止一切顺利!
  • 根据我在错误中看到的内容,我可能会在情节提要上寻找未命名的内容,可能是 textView。
  • stackoverflow.com/questions/11135047/… 中说 self 是视图控制器。我会检查 self 是否需要成为视图控制器。在那个范围内你的 tableview self 中可能是一个 table view 但老实说我不确定没有以这种方式实现一个 tableview。
猜你喜欢
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 2012-11-18
  • 1970-01-01
相关资源
最近更新 更多