认为没有传递正确的值(值“卡住”)

时间:2014-05-28 08:32:16

标签: ios objective-c segue viewcontroller

大家好,我遇到过segue传递的问题;基本上是一个旨在根据内容进行更改的值,例如,ViewControllerA在传递到ViewControllerB时不会发生变化。我宁愿迷失在这个问题上,所以请原谅我不知道该怎么称呼它。

好吧,让我们来看看。

我将总结我正在构建的应用程序结构,以帮助您更好地了解这里到底发生了什么。我正在构建一个方程计算器,它使用表VC的决策树网络来传递值,改变较低VC显示的选项。层次结构是这样的:

// Top level:

- Main Menu View Controller (UIButton-triggered segue to calculator stack)
- ViewController (user selects type of equation out of array, it is passed into segue via indexPath.row)
- Equation View Controller (user selects individual equation, pass-thru via indexPath.row)
- DerivationViewController (discussed in detail below - source of issue)
- Calculator VC (actual calculating endpoint)

// Bottom level

DerivationViewController基本上执行以下操作:

// Declared in .h and fed by upstream VC:
@property (nonatomic, strong) NSString *segueInput;

// .m
@implementation DerivationViewController()
{
     NSArray* derivations;
     NSDictionary* sDict;
}

-(void) viewDidLoad:
{
     // First, the code discriminates against the fed-thru segue value to determine the options that will appear in the table
     // cells
     if (segueInput == @"Something")
     {
          derivations = [[NSArray alloc] initWithObjects: @"Add", @"Some", @"Options", nil];
     }

     // Then, it reads into the plist. The plist consists of a dictionary of dictionaries. The plist uses segueInput to 
     // discriminate and puts the appropriate dictionary into the ivar "sDict" to allow transport across methods.
     NSString *filePath = [[NSBundle mainBundle] pathForResource: @"myPlist" ofType:@"plist"];
     NSDictionary * mDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
     sDict = [mDict objectForKey: segueInput];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"calculation"])
    {
         // The indexPath.row then selects the appropriate number (string-encoded, i.e. @"102") to put into the downstream
         // VC's NSString* derivationNumber property.
         NSIndexPath *indexPath = [self.tableView3 indexPathForSelectedRow];
         CalcController *destViewController = segue.destinationViewController;
         destViewController.derivationNumber = [sDict objectForKey:[derivations objectAtIndex:indexPath.row]];
    }
}

现在这里的事情不会满足。问题是我注意到无论选择什么派生,单位/ VC设置(由下游VC中的plist选择)都是相同的。也就是说,

对于等式a = b * c,三个推导将是

    a = b * c
    b = a / c
    c = a / b

无论派生什么末端变量,错误都会显示值b和c。

所以我在那里扔了一个NSLog来帮助我了解发生了什么,阅读类似的内容:

 NSLog(@"Forward VC reports that sDict passthrough value is: %@", [sDict objectForKey:[derivations objectAtIndex:indexPath.row]]);

举一个例子,在完整的VC源中有42个方程式。这些方程中的每一个都列出了它们的推导,对于方程#1,有三个推导,所以通过的数字应该是1,2或3.正在发生的是无论我选择哪种推导,我得到数字1来自NSLog的终端输出上的segue。

无论推导如何,它都保持不变。我想知道,并且我愿意考虑,如果第一次加载计算VC,它会“中毒”该值并且不刷新它,在迭代中保持它,这就是标题引用的原因“释放视图控制器”。这可能是一个可能的问题吗?

编辑:

以下是我从derivationNumber内访问CalcController的方式。当我对Objective-C更新并且还在学习时,我写了这段代码;我没有想到使用[NSString intValue]调用然后使用switch()进行清理。你被警告了。

-(IBAction)Calculate:(id) sender内(映射到Calculate UIButton):

if ([_derivationNumber isEqualToString:@"1"])
{
    // Equation 1: 1/42:
    float a = [self.variable1.text floatValue];
    float b = [self.variable2.text floatValue];
    float ansnum = a*b;
    NSString *ans = [NSString stringWithFormat:@"%f", ansnum];
    self.results.text = ans;
}
// etc.

1 个答案:

答案 0 :(得分:0)

我建议将所选行保存到didSelectRowAtIndexPath方法中的属性,如果有一个,则从故事板中的单元格中删除segue,因为该行的实际选择状态可能受到segue的影响

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Store the selected indexPath
    self.selectedDerivation=indexPath.row;
    [self performSegueWithIdentifier:@"calculation" sender:self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"calculation"])
    {
         // The indexPath.row then selects the appropriate number (string-encoded, i.e. @"102") to put into the downstream
         // VC's NSString* derivationNumber property.
         CalcController *destViewController = (CalcController *)segue.destinationViewController;
         destViewController.derivationNumber = [sDict objectForKey:[derivations objectAtIndex:self.selectedDerivation]];
    }
}