【问题标题】:Why I can't pass data using segue为什么我不能使用 segue 传递数据
【发布时间】:2013-07-04 23:30:11
【问题描述】:

我有一个包含 key 和 value 的 plist...让我们说:

key: alpha_male_body_language
value: Alpha Male Body Language
key: building_attraction
value: Building Attraction
key: fifteen_lessons
value: Fifteen Lessons
key: how_can_it_have_gone_wrong
value: How can It Have Gone Wrong

这是我对 tableview 的实现:

#import "BookTitleViewController.h"
#import "BookLessonViewController.h"

@interface BookTitleViewController ()

@end

@implementation BookTitleViewController{
    NSDictionary *bookTitles;
}

@synthesize tableView;
@synthesize bookTitles;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.bookTitles = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"]];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

    self.bookTitles = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RecipeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    NSString *value = [[self.bookTitles allValues] objectAtIndex:indexPath.row];

    //cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
    cell.textLabel.text = value;

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowBookLesson"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        BookLessonViewController *destViewController = segue.destinationViewController;

        destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
        destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];

        //destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
    }
}

@end

我使用上面的代码有两个问题:

  1. 从 plist 读取列表后,列表未按顺序显示。例如标题“怎么会出错”显示在第一个列表中,知道它在第 4 个列表中。

  2. 我得到异常说:

    -[UINavigationController setBookTitleValue:]:无法识别的选择器发送到实例 0x894c230

这是指行:

 destViewController.bookTitleKey = [[self.bookTitles allKeys]     objectAtIndex:indexPath.row];
 destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];

(在 prepareForSegue 函数内部)。

请帮我解决这个问题。谢谢并感激不尽!

【问题讨论】:

  • Abizem's answer below 适合您的第一个问题。对于您的第二个问题,您的 segue 的destinationViewController 可能是一个UINavigationController,它包含您的BookLessoniewController 作为其顶级VC。请参阅my suggested approach 处理此问题。
  • 查看这个答案以对 NSDictionary 进行排序并转换为 NSArray。 stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary
  • 嗨埃斯克。我仍然不知道如何解决 2 号问题。您能否指出您的“建议方法”中的哪一部分可以解决我的问题 2 号?我还是新手,刚学ios 3天... =)

标签: ios objective-c segue property-list


【解决方案1】:

有关问题 2 可能解决方案的更多详细信息:

  1. UIStoryboardSegue+MMNavigationController.h 添加到您的项目中,在this Github repo 中找到
  2. 在视图控制器的实现文件中导入该头文件
  3. prepareForSegue:sender: 的实现中更改一行,如下所示:
BookLessonViewController *destViewController = segue.topLevelDestinationViewController;

解释:如果故事板segue 指向的目标视图控制器包含在UINavigationController 中,那么segue 的destinationViewController 的值将是导航控制器对象。此处引用的代码将通过检查destinationViewController 是否是UINavigationController 类的实例来处理此问题,如果是,则将返回其topViewController。这都是在UIStoryboardSegue 类上实现的; categories 是一种向现有类添加方法或属性以扩展其功能而无需使用继承等的方法。

【讨论】:

  • 这行得通!你能解释一下那里发生了什么吗?在这种情况下 category.h 的 sn-p 做了什么?
  • 非常感谢,您为我节省了很多时间。
【解决方案2】:

看起来您的 plist 用于生成字典。 NSDictionary 中的键(以及因此的值)没有排序。所以你取出钥匙的顺序是不固定的。

因此,当您假设键和值位于特定位置时,这是一个错误的假设。

您应该使用数组而不是字典作为数据源。

【讨论】:

  • 很酷的洞察力!!谢谢!您是否有任何解决方案让对象具有键和值,但在第一次插入对象时以某种方式排序,那将是第一个对象。谢谢。
  • 给它一个存储订单的密钥
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2021-11-09
  • 2019-09-19
  • 2015-05-24
相关资源
最近更新 更多