gpt4 book ai didi

ios5 - 使用 StoryboardiOS5传递对象

转载 作者:行者123 更新时间:2023-12-02 09:29:37 27 4
gpt4 key购买 nike

我正在尝试将一个对象传递给我在 Storyboard中创建的静态分组 TableView 。

这是我在第一个 View 中使用的代码来推送第二个 View :

- (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 是正确的,但是当推送详细信息 View 时,detailView 中的单元格为空。我可能只是错过了一些愚蠢的东西,但如果有一双新的眼睛,我将不胜感激。

这里是detailView Controller 的代码:

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

最佳答案

那么,您已经使用 Storyboard创建了两个 View (第一个 TableView 和 CustomerDetailTableViewController)?在这种情况下,您必须单击 Storyboard中两个 View 之间的连接线,并将“ Storyboard转场”部分中的“标识符”字段设置为“setCustomer”之类的内容。这是一个屏幕截图:这是一个小屏幕截图:

enter image description here

之后,你可以在第一个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"

位于实现文件的顶部。

希望对您有所帮助!

关于ios5 - 使用 StoryboardiOS5传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com