首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用/触发PrepareForSeque

未调用/触发PrepareForSeque
EN

Stack Overflow用户
提问于 2013-05-28 02:09:55
回答 1查看 371关注 0票数 1

C/xcode极客!

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

请参见代码:

代码语言:javascript
复制
//
//  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)。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-28 02:40:51

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

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

尤其是从他在那篇文章中的回答中可以看出:

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

代码语言:javascript
复制
[self performSegueWithIdentifier:@"viewB" sender:self];

--

当然是换了你的段名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16778452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档