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

标签 c# ios uitableview uiview xamarin

我携带一个带有对象集合的 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/

相关文章:

ios - 如何从不同的 Xcode 版本复制模拟器?

ios - 在 iOS 7 中隐藏 UISearchBar 裁剪顶行

c# - 如何在控件之间添加动态间距? (附图片)

objective-c - 如何在 Objective-C 中创建两个模型类的包装类

c# - 更改转发器行值?

ios - 如何更改 Sprite 图像和变量

ios - 如何在循环中设置 UIButtons 的 Action ?

ios - reloadRowsAtIndexPaths 搞乱了 animateWithDuration

c# - Windows 窗体复选框已更改还是已更新?

c# - 将业务对象标记为脏?