gpt4 book ai didi

ios - 需要从 iOS 中的 tableView 选择动态创建 ViewController 对象

转载 作者:行者123 更新时间:2023-11-29 11:05:25 24 4
gpt4 key购买 nike

我的 iOS 应用程序中有一个 UIViewController,它显示一个从 NSMutableArray 派生的表格。此表中的每个单元格都引用一个唯一的 UIViewController,当用户进行选择时调用该控件。我在“didSelectRowAtIndexPath:”方法中尝试做的是通过 NSMutableDictionary 动态创建 UIViewController,其中包含与 NSMutableArray< 中的值匹配的键 构建表的基础,以及包含需要调用的相应 UIViewController 的相应类名称的值。因为列表相当长,所以我想我需要使用 for 循环来执行此操作,但我对如何操作有点困惑。我的 NSMutableDictionary 看起来像这样:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"aViewController" forKey:@"SelectionA"];
[dict setObject:@"bViewController" forKey:@"SelectionB"];
[dict setObject:@"cViewController" forKey:@"SelectionC"];

我的 NSMutableArray 是我的 TableView 的基础,如下所示:

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"SelectionA", @"SelectionB", @"SelectionC",...,nil];

我如何获取对单元格内值的引用,然后构建一个 for 循环,动态创建与用户从 tableView 中所做的选择相对应的正确 viewController,然后将用户带到该 viewController通过导航 Controller ?

提前感谢所有回复的人。

最佳答案

创建多个 ViewController 不是一个好主意,你应该创建一个 ViewController,并将 tableCell 的值传递给它。换句话说,您可以通过选择单元格来更改 ViewController 的数据模型。但是您将呈现相同的 ViewController。

只有在您的单元格对应不同类型的情况下(例如,一个单元格是路毛,另一个单元格是文本值),您必须调用不同的 ViewController。

如果你真的需要不同的 View Controller ,那么从单元格数据中获取你想要显示的类型

didSelectCellRowAtIndexPath

myAppDelegate *appDelegate = 
[[UIApplication sharedApplication] delegate];

UIViewController *viewControllerToDisplay;
switch (selection.type) {
case MapType:
viewControllerToDisplay = appDelegate.mapViewController;
case Picture:
viewControllerToDisplay = appDelegate.pictureViewController;

}

现在将 viewControllerToDisplay 推送到导航 Controller 。

关于ios - 需要从 iOS 中的 tableView 选择动态创建 ViewController 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13866342/

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