xcode 在表视图中被选中后如何将数据传递到详细视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7562258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data to detail view after being selected in a table view?
提问by tarheel
I have a UITableViewController that has data populated from an NSMutableArray called userList. When specific user is selected, it goes to a detail view, and updates the title in the NavBar, but it wont pass the data to update a UILabel in the UIView.
我有一个 UITableViewController,其中包含从名为 userList 的 NSMutableArray 填充的数据。When specific user is selected, it goes to a detail view, and updates the title in the NavBar, but it wont pass the data to update a UILabel in the UIView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//Attempt at a singleton to pass the data
DetailView *details = [[DetailView alloc] init];
details.userNameLbl = [userList objectAtIndex:indexPath.row];
DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];
//This line doesn't pass the data
detailVC.userNameLbl.text = [userList objectAtIndex:indexPath.row];
//This line does update the title in the NavBar
detailVC.navigationItem.title = [userList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
[details release];
[detailVC release];
}
Should I be trying to push the data from the table view to the detail view, or have the detail view try to pull the data from the table view?
我应该尝试将数据从表视图推送到详细视图,还是让详细视图尝试从表视图中提取数据?
DetailView.h has the following line that is in fact hooked up in IB.
DetailView.h 的以下行实际上已连接到 IB 中。
IBOutlet UILabel *userNameLbl
回答by Ashley Mills
The userNamelbl
isn't instantiated until the view is loaded from the nib file. This doesn't happen immediately at initialisation, but will have happened by the time viewDidLoad
is called.
在userNamelbl
从 nib 文件加载视图之前不会实例化。这不会在初始化时立即发生,但会在viewDidLoad
调用时发生。
So, you should to declare a property in DetailView to store your title, and then assign that value to userNamelbl.text
in the viewDidLoad
method.
因此,您应该在 DetailView 中声明一个属性来存储您的标题,然后将该值分配给userNamelbl.text
该viewDidLoad
方法。
For example, in your table viewController:
例如,在您的表视图控制器中:
DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];
detailVC.userName = [userList objectAtIndex: indexPath.row];
and in your detail viewController:
并在您的详细视图控制器中:
- (void) viewDidLoad
{
[super viewDidLoad];
self.userNameLbl.text = self.userName;
}
The viewController's navigationItem property is created when the viewController is initialised, hence you can assign to the navigationItem.title
immediately.
viewController 的 navigationItem 属性是在 viewController 初始化时创建的,因此您可以navigationItem.title
立即分配给。
Swift code
SWIFT代码
let detailVC = DetailView(nibName: nil, bundle: nil)
detailVC.userName = userList.objectAtIndex(indexPath.row) as? NSString
and
和
class DetailView: UIViewController {
@IBOutlet var userNameLbl: UILabel
var userName:NSString?
override func viewDidLoad() {
super.viewDidLoad()
self.userNameLbl.text = self.userName
}
}
回答by nekno
Are you mixing UIViews
and UIViewControllers
? You should have a DetailViewController
class that inherits from UIViewController
or some sublcass (like UITableViewController
); it will have DetailViewController.m
and DetailViewController.h
files to declare and define your class. It should have a corresponding nib that defines the UIView
that the UIViewController
loads; it will have a DetailView.xib
file.
你在混合UIViews
和UIViewControllers
?您应该有一个DetailViewController
继承自UIViewController
或某些子类(如UITableViewController
)的类;它必须DetailViewController.m
和DetailViewController.h
文件来声明和定义类。它应该有一个相应的笔尖定义UIView
的UIViewController
负荷; 它将有一个DetailView.xib
文件。
You can't assign the value to the UILabel
directly because UIView
hasn't been loaded at the time you need to assign the user name value.
你不能UILabel
直接赋值给 ,因为UIView
在你需要赋值用户名值的时候还没有加载。
In order to do what you want, you should declare a public property (userName
) to "push" the value onto the detail view controller from the master controller. Once the detail view is loaded, it can assign the value from the property to the label and nav bar.
为了做您想做的事,您应该声明一个公共属性 ( userName
) 以将值从主控制器“推送”到详细视图控制器上。加载详细视图后,它可以将属性值分配给标签和导航栏。
In your master view controller (UITableViewController
):
在您的主视图控制器 ( UITableViewController
) 中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
DetailViewController *detailVC = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
detailVC.userName = [userList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
In your detail view controller:
在您的详细视图控制器中:
DetailViewController.h
细节视图控制器.h
@property (retain) NSString* userName;
@property (nonatomic, retain) IBOutlet UILabel *userNameLbl;
DetailViewController.m
DetailViewController.m
@synthesize userName;
@synthesize userNameLbl;
-(void) viewDidLoad {
[super viewDidLoad];
self.userNameLbl.text = self.userName;
self.navigationItem.title = self.userName;
}
回答by lxt
Presumably your DetailView
requires the data from your selected row to function?
大概您DetailView
需要所选行中的数据才能运行?
If so, I'd say the 'correct' approach would be to create a custom init
method that passed in the data you required. For example:
如果是这样,我会说“正确”的方法是创建一个自定义init
方法,传入您需要的数据。例如:
[[DetailView alloc] initWithTitle:(NSString *)title text:(NSString *)text]
There's nothing inherently wrong with your approach, but passing the data the view controller requires at its creation is architecturally better (in my opinion, I'm sure someone will disagree!). For example, think of a table view controller - you pass in the table view style in the init
method, you don't set it later on. Same for a UIImageView
- you have an initWithImage
method that lets you set the image at creation.
您的方法没有本质上的错误,但是传递视图控制器在创建时所需的数据在架构上更好(在我看来,我相信有人会不同意!)。例如,考虑一个表视图控制器 - 您在init
方法中传入表视图样式,稍后不会设置它。与 a 相同UIImageView
- 您有一种initWithImage
方法可以让您在创建时设置图像。