【问题标题】:Passing object using story board iOS5使用情节提要 iOS 5 传递对象
【发布时间】:2011-12-27 12:48:31
【问题描述】:

我正在尝试将一个对象传递给我在故事板中创建的静态分组表视图。

这是我在第一个视图中用于推送第二个视图的代码:

- (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];

    }

firstName 的 NSlog 是正确的,但是当推送详细视图时,详细视图中的单元格为空。我可能只是错过了一些愚蠢的东西,但我会非常感谢你有一双全新的眼睛。

这里是 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

【问题讨论】:

    标签: ios5 storyboard


    【解决方案1】:

    那么,您已经创建了带有情节提要的两个视图(第一个 TableView 和 CustomerDetailTableViewController)? 在这种情况下,您必须单击故事板中两个视图之间的连接线,并将“标识符”字段设置为“故事板 Segue”部分,类似于“setCustomer”。这是一个屏幕截图: 这里有一个小截图:

    之后可以在第一个TableView上注释tableView:didSelectRowAtIndexPath:方法,替换成这个方法:

    - (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"
    

    在实现文件的顶部。

    希望对你有所帮助!

    【讨论】:

    • 非常好的答案。但是,我必须在索引路径中添加.row 才能获得数组的正确索引(我认为这是因为我使用的是分组视图)
    猜你喜欢
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 2013-11-30
    • 2023-03-03
    • 2011-12-26
    • 2012-09-22
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多