2013-05-27 49 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