【问题标题】:Detailed storyboard with 3 issued with [closed]详细的故事板与 3 发布 [关闭]
【发布时间】:2013-04-30 15:17:13
【问题描述】:

在详细情节提要的 Xcode 中实现,其中 3 个根据 id 与 Tableview 一起发布。 表中的行具有名称和 ID(int 值)

If id= 1  i need load OneViewController
If id= 2 i need load TwoViewController
If id= 3 i need load ThreeViewController

我使用 Storyboard 中的导航控制器

【问题讨论】:

  • 很好,但问题出在哪里?

标签: ios xcode uitableview storyboard detailview


【解决方案1】:

您需要将 viewControllers 添加到情节提要中,并为它们提供与其名称相同的 storyboardId。

创建一个 dataSourceArray 来保存具有 Id 和 Name 值的字典对象

#define kItemId @"Id"
#define kItemTitle @"Name"

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSourceArray = @[@{kItemId: @1,kItemTitle:@"First"},
                             @{kItemId: @2,kItemTitle:@"Second"},
                             @{kItemId: @3,kItemTitle:@"Third"}];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = self.dataSourceArray[indexPath.row][kItemTitle];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataSourceArray[indexPath.row];

    NSUInteger tag = [dict[kItemId]integerValue];

    NSString *identifier = nil;

    switch (tag) {
        case 1:
        {
            identifier = @"OneViewController";
            break;
        }
        case 2:{
            identifier = @"TwoViewController";
            break;
        }
        case 3:{
            identifier = @"ThreeViewController";
            break;
        }

        default:
            break;
    }

    UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
    viewController.title = identifier;

    [self.navigationController pushViewController:viewController
                                         animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Source Code

【讨论】:

  • 非常感谢您的帮助。现在但是出现了一个没有序列的新问题作为详细视图中的给定变量。
  • @PetroNepyivoda 抱歉,您能重新表述一下您的问题吗?
  • 我想从 ViewController 向 DetailView 传输 wariable
  • Like Seque OneViewController *detail = [???????????]; detail.Name = self.dataSourceArray[indexPath.row][kItemTitle]; - 例如在 OneViewController @property (nonatomic, retain) NSString *Name;
  • @PetroNepyivoda 现在我知道了,您可以在 switch 语句中移动 viewController 的实例化。您能否提及您要分配的具体数据是什么?
猜你喜欢
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多