简体   繁体   English

如何在表视图中选择后将数据传递到详细视图?

[英]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 ? 你在混合UIViewsUIViewControllers吗? 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.mDetailViewController.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.

相关问题 如何将数据从分区表视图传递到详细信息视图? - how to pass data from sectioned table view to detail view? 如何在详细视图中传递节表视图数据 - how to pass section table views data in detail view 如何在详细视图中显示与表中所选行相对应的标题 - How to display the title in detail view corresponding to the row selected in the table 如何将seque ios5 storyboard uitableview中的数据传递给详细视图 - How pass data in seque ios5 storyboard uitableview to detail view 如何将数据从详细视图控制器传递回uitableview? - How to pass data back from detail view controller to uitableview? iOS 6将数据从TableView传递到详细信息视图 - iOS 6 pass data from TableView to Detail View 在Modal View控制器编辑数据源(数组)之后,如何重新加载表视图 - How to reload a table view, after data source (array) being edited by a Modal View controller 在表中选择行时,如何通知详细视图选择了哪一行? - When a row is selected in a table, how can I inform the detail view about which row was selected? 弹出详细信息视图后自动取消选择表视图单元格? - Automatic deselection of table view cell after popping detail view? 从“详细视图”返回后如何刷新表格视图? - How do I refresh a table view after returning from a “detail view?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM