[英]How to pass data to detail view after being selected in a table view?
I have a UITableViewController that has data populated from an NSMutableArray called userList. 我有一个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.
选择特定用户后,它将转到详细视图,并更新NavBar中的标题,但它不会传递数据以更新UIView中的UILabel。
- (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
The userNamelbl
isn't instantiated until the view is loaded from the nib file. 在从nib文件加载视图之前,不会实例化
userNamelbl
。 This doesn't happen immediately at initialisation, but will have happened by the time viewDidLoad
is called. 这不会在初始化时立即发生,但会在调用
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中声明一个属性来存储您的标题,然后将该值分配给
viewDidLoad
方法中的userNamelbl.text
。
For example, in your table viewController: 例如,在你的表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
}
}
Are you mixing UIViews
and UIViewControllers
? 你在混合
UIViews
和UIViewControllers
吗? You should have a DetailViewController
class that inherits from UIViewController
or some sublcass (like UITableViewController
); 你应该有一个继承自
UIViewController
或某个sublcass(如UITableViewController
)的DetailViewController
类; it will have DetailViewController.m
and DetailViewController.h
files to declare and define your class. 它将具有
DetailViewController.m
和DetailViewController.h
文件来声明和定义您的类。 It should have a corresponding nib that defines the UIView
that the UIViewController
loads; 它应该有一个相应的nib,它定义了
UIViewController
加载的UIView
; it will have a DetailView.xib
file. 它将有一个
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. 为了做你想做的事,你应该声明一个公共属性(
userName
),将值从主控制器“推”到详细视图控制器上。 Once the detail view is loaded, it can assign the value from the property to the label and nav bar. 加载详细信息视图后,它可以将属性中的值分配给标签和导航栏。
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 DetailViewController.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;
}
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. 如果是这样,我会说“正确”的方法是创建一个传递所需数据的自定义
init
方法。 For example: 例如:
[[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. 例如,考虑一个表视图控制器 - 您在
init
方法中传递表视图样式,稍后不再设置它。 Same for a UIImageView
- you have an initWithImage
method that lets you set the image at creation. 对于
UIImageView
- 您有一个initWithImage
方法,可以让您在创建时设置图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.