简体   繁体   English

如何通过prepareForSegue编辑我的segue目标数据?

[英]How do I edit my segue target data through prepareForSegue?

I am trying to edit the segue's target's data through prepareForSegue , but everything I try keeps returning the "unrecognized selector sent to instance ..." error. 我正在尝试通过prepareForSegue编辑segue目标的数据,但是我尝试执行的所有操作始终返回“发送给实例的无法识别的选择器...”错误。 Is there a special way to edit my target's data, is it being deleted before I edit it or something like that? 有没有一种特殊的方法可以编辑目标数据,在我编辑目标数据之前将其删除还是类似的方法? I just started picking this stuff up so I still don't have my head all the way around it. 我刚开始捡起这些东西,所以我仍然一无所知。 Here is a sample of some of my code 这是我的一些代码示例

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"managerSegue"]){
    ASExpenseList *target = [segue destinationViewController];
    NSIndexPath *row = [self.tableView indexPathForSelectedRow];
    self.cellPressed = row.row;

    switch(self.cellPressed){
        case 0:
            [target initWithData:self.oneTimeExpenses]; //PROBLEM LINE
           // target.expenses = [[NSMutableArray alloc] initWithArray:self.oneTimeExpenses]; ALSO PROBLEM
            target.title = @"One-Time"; //not breaking the program but not working

The ASExpenseList function I'm calling is really simple: 我要调用的ASExpenseList函数非常简单:

-(void) initWithData:(NSMutableArray *)newExpenses {
self.expenses = newExpenses;
[self.tableView reloadData];
}

So I am trying to set the target's data to some data I already have, and I am trying to dynamically change the title. 因此,我试图将目标的数据设置为我已经拥有的某些数据,并且试图动态更改标题。 The title change isn't working and the data lines are throwing errors. 标题更改无效,数据行引发错误。 I'm guessing that I just misunderstand how it works, so any help or clarification would be awesome. 我猜想我只是误解了它的工作原理,因此任何帮助或澄清都将是很棒的。 Thanks! 谢谢!

When you perform a segue, the storyboard handles the alloc + init of a UIViewController, so you don't need to do the initialization. 当执行segue时,情节提要会处理UIViewController的alloc + init,因此您无需进行初始化。 You can set data by using the properties. 您可以使用属性设置数据。

Can you show the actual "unrecognized selector sent to instance ..." error you're getting? 您能显示出您收到的实际“无法识别的选择器发送给实例...”错误吗? Different things can be going wrong here depending on what selector is being sent to what object. 根据将选择器发送到哪个对象的不同,这里可能会出错。

But generally, when you're dealing with "unrecognized selector", step 0 is to determine if the object is really what you think it is. 但是通常,当您处理“无法识别的选择器”时,第0步将确定对象是否确实是您认为的真实对象。 In this case, make sure [segue destinationViewController] really is an ASExpenseList . 在这种情况下,请确保[segue destinationViewController]确实是ASExpenseList I have my doubts, as ASExpenseList sounds like a model or a view, and [segue destinationViewController] as the name implies, returns a view controller. 我很怀疑,因为ASExpenseList听起来像是模型或视图,顾名思义, [segue destinationViewController]返回一个视图控制器。

Some strategies for verifying the identity of the destinationViewController include: 验证destinationViewController身份的一些策略包括:

  • NSLog("dest: %@", [segue destinationViewController]);
  • NSLog("dest class: %@", [segue destinationViewController]);
  • Placing a breakpoint and examining target in Variables View. 在变量视图中放置一个断点并检查target
  • Placing a breakpoint and interrogating target with po in LLDB. 在LLDB中放置一个断点并用po询问target

Add the newExpenses as a property in your ASExpenseList class. newExpenses作为属性添加到ASExpenseList类中。 Set it in your prepareForSegue and populate the tableView in your viewDidLoad in ASExpenseList using the NSArray you passed. 在您的prepareForSegue设置,并使用传递的NSArray在ASExpenseListviewDidLoad中填充tableView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM