当在表中选择项目时,传递加载在 plist 中命名的 .xib


嗨,我

正在尝试更新我正在开发的应用程序。最初,该应用程序是一个五视图选项卡视图应用程序。我决定要修改应用程序,使其具有更少的选项卡,因此我想将三个选项卡移动到表列表中并从那里加载它们,从而释放一些选项卡栏空间用于我想实现的其他一些事情。

为了实现这一点,我想我会设置一个表视图并将详细信息从 .plist 加载到表视图中。我想我可以在 .plist 中命名 .xib 文件,然后获取这些名称并使用它们将适当的 .xib 文件推送到视图中。从理论上讲,这一切听起来都不错(至少在我的脑海中),但我在这样做时遇到了一些问题。我可以使 .plist 在创建表视图方面正常工作,但我没有卡住,因为我不知道如何将笔尖的名称传入并使用它来推送正确的视图。谁能向我解释一下。我的列表看起来像这样:

<array>
<dict>
    <key>rowData</key>
    <array>
        <dict>
            <key>calcType</key>
            <string>Item 1</string>
            <key>subdetails</key>
            <string>some stuff about item 1</string>
            <key>nibName</key>
            <string>nibOne.xib</string>
        </dict>
        <dict>
            <key>calcType</key>
            <string>Item 2</string>
            <key>subdetails</key>
            <string>some stuff about item 2</string>
            <key>nibName</key>
            <string>nibTwo.xib</string>
        </dict>
        <dict>
            <key>calcType</key>
            <string>Item 3</string>
            <key>subdetails</key>
            <string>some stuff about item 3</string>
            <key>nibName</key>
            <string>nibThree.xib</string>
        </dict>
    </array>
</dict>

我的didSelectRowAtIndex方法目前看起来像这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

     //Get the dictionary of the selected data source.
NSDictionary *dictionary = [[[self.artCalculators     objectAtIndex:indexPath.section]objectForKey:@"rowData"] objectAtIndex:indexPath.row];
CalcDetailViewController *controller = [[CalcDetailViewController alloc] initWithNibName:@"CalcDetailViewController" bundle:[NSBundle mainBundle]]; 
 controller.title = [dictionary objectForKey:@"calcType"];
 [self.navigationController pushViewController:controller animated:YES];

我知道需要改变的行是:

CalcDetailViewController *controller = [[CalcDetailViewController alloc] initWithNibName:@"CalcDetailViewController" bundle:[NSBundle mainBundle]];

但我真的不确定它需要什么。谁能告诉我?

你从

字典中获取笔尖名称的方式与从字典中获取任何其他对象的方式相同:

NSString *nibName = [dictionary objectForKey:@"nibName"];
CalcDetailViewController *controller = [[CalcDetailViewController alloc] initWithNibName:nibName bundle:nil];

您不需要显式传入主捆绑包,如果您传入 nil,这是默认值。

最新更新