2011-11-12 80 views
2

我想將對象傳遞給我在故事板中創建的靜態分組表格視圖。使用故事板iOS5傳遞對象

下面是我用我的第一個觀點推動第二視圖代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSLog(@"Row Selected"); 
     CustomerDetailTableViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:@"DetailsView"]; 
     detailView.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]]; 
     NSLog(@"%@",detailView.customer.firstName); 
     [self.navigationController pushViewController:detailView animated:YES]; 

    } 

的的NSLog與firstName是正確的,但在細節視圖被推細胞中的DetailView爲空。我可能只是失去了一些愚蠢的東西,但一個新的眼睛將不勝感激。

下面是的DetailView控制器代碼:

CustomerDetailTableViewController.h

@class Customer; 

#import <UIKit/UIKit.h> 

@interface CustomerDetailTableViewController : UITableViewController{ 
    Customer *customer; 

    UILabel *fullName; 
    UILabel *address; 
    UILabel *homePhone; 
    UILabel *cellPhone; 
    UILabel *email; 

} 

@property (nonatomic, strong) IBOutlet UILabel *fullName; 
@property (nonatomic, strong) IBOutlet UILabel *address; 
@property (nonatomic, strong) IBOutlet UILabel *homePhone; 
@property (nonatomic, strong) IBOutlet UILabel *cellPhone; 
@property (nonatomic, strong) IBOutlet UILabel *email; 
@property (nonatomic, strong) Customer *customer; 
@end 

CustomerDetailTableViewController.m

#import "CustomerDetailTableViewController.h" 
#import "Customer.h" 

@implementation CustomerDetailTableViewController 
@synthesize fullName, address, homePhone, cellPhone, email, customer; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 

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

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 

    fullName = [NSString stringWithFormat:@"%@ %@", customer.firstName, customer.lastName]; 
    address = [NSString stringWithFormat: @"%@/n%@, %@ %@", customer.address, customer.city, customer.state, customer.zipCode]; 

    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

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

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

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

回答

12

所以,你已經創建了兩個視圖(第一和TableView中CustomerDetailTableViewController)與故事板? 在這種情況下,您必須點擊兩個視圖之間的連接線進入故事板,並將「標識符」字段設置爲「故事板塞格」部分,類似「setCustomer」。下面是截圖: 這一點截圖:

enter image description here

之後,你可以發表評論的方法的tableView:didSelectRowAtIndexPath方法:第一個的TableView,並用這種方法代替:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"setCustomer"]) { 
     CustomerDetailTableViewController *customerDetailVC = (CustomerDetailTableViewController *)[segue destinationViewController]; 
     customerDetailVC.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]]; 
    } 
} 

記住包括

#include "CustomerDetailTableViewController.h" 

位於實現文件的頂部。

我希望這個幫助!

+0

非常好的答案。然而,我不得不在索引路徑中添加'.row'來獲得數組的正確索引(我認爲這是因爲我使用了分組視圖) –