gpt4 book ai didi

c# - 如何使用 segue 传递对象

转载 作者:行者123 更新时间:2023-12-01 20:14:46 64 4
gpt4 key购买 nike

我携带一个带有对象集合的 uitableview。
当我点击一个 UITableViewCell 打开另一个 UIView。
但我想为这个新的 UIView 发送 UITableViewCell 中的对象并在那里显示它们的详细信息。
我跟着回答这个问题来加载另一个 View :ViewController Segue Xamarin
如果有人帮助,我将不胜感激

enter image description here

最佳答案

有几个解决方案,第一个是使用
prepareForSegue(_:sender:) 方法:

Discussion The default implementation of this method does nothing; you can override it to pass relevant data to the new view controller or window controller, based on the context of the segue. The segue object describes the transition and includes references to both controllers involved in the segue.

Segues can be triggered from multiple sources, so use the information in the segue and sender parameters to disambiguate between different logical paths in your app. For example, if the segue originated from a table view, the sender parameter would identify the cell that the user clicked. You could use that information to set the data on the destination view controller.



在这种情况下,您需要“保存”要传入 didSelectRow 的对象:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentObjectToPass = .. some object from array or somewhere else..
}

然后将其设置为下一个vc:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {
if ([segue.identifier isEqualToString:@"bookingDetailsSegue"]) {
self.nextVC = segue.destinationViewController;
self.nextVC.objectToPass = self.currentObjectToPass;
}
}

另一种方式是拒绝使用segue,通过storyboardID获取vc。

这种方式你需要
instanceViewController(withIdentifier:) 方法:

Return Value The view controller corresponding to the specified identifier string. If no view controller is associated with the string, this method throws an exception.

Discussion You use this method to create view controller objects that you want to manipulate and present programmatically in your application. Before you can use this method to retrieve a view controller, you must explicitly tag it with an appropriate identifier string in Interface Builder.

This method creates a new instance of the specified view controller each time you call it.



然后您的代码将如下所示:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"nextVCID"];

self.nextVC.objectToPass = = .. some object from array or somewhere else..
[self.navigationController pushViewController:self.nextVC animated:YES];
}

并且不要忘记在 IB 中设置 vc 标识符(Storyboard ID):

enter image description here

关于c# - 如何使用 segue 传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37036927/

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