【问题标题】:Backward compatibility issue with iOS Segue Data transferiOS Segue 数据传输的向后兼容性问题
【发布时间】:2017-08-10 06:23:01
【问题描述】:

我发现一些运行 iOS 9.3.5 的旧硬件存在一个奇怪的问题,但不是所有的旧硬件。我非常感谢任何关于为什么会发生这种情况的想法。我知道我可以使用单例全局解决这个问题,但这看起来很丑陋。

在糟糕的情况下,通过 segue 进行的数据传输不会到达新的视图控制器,而在其他运行 9.3.5 的旧硬件和所有新硬件的情况下,一切都按预期工作。

简而言之,我有一个应用程序,它使用我使用 Interface Builder(不是标准模板)构建的主/详细设计。主 ViewController 包括一个 UITableView 并且还使用 STCollapse 和 MMDrawerSideSlide 库。选择表格行会生成一个 segue,其中 prepareForSegue 将一些项目加载到 DetailViewController 的属性中。 NSString 属性总是可以到达,但在某些旧硬件中(见下文),所选行的 NSIndexPath 正确移动到主 VC 中 prepareForSegue 中的详细属性,但没有到达 DetailViewController 并且该属性为 NIL 时细节的 viewDidLoad 执行。

DetailViewController中NSIndexPath的硬件和iOS版本的值表

iPad 2       9.3.5   nil
iPad Air     9.3.5   nil
iPad Air2    9.3.5   correct
iPhone 5     9.3.5   nil
iPhone 5s    9.3.5   correct
iPhone 6     9.3.5   correct
all devices  10.x    correct

代码片段

主视图控制器.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    [self performSegueWithIdentifier:@"toDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"toDetail"])
    {
        DetailViewController *detailVC = segue.destinationViewController;
        detailVC.selectedIndex = self.selectedIndexPath;
        detailVC.searchName = _theName;
    }
}


DetailViewController.h
@property (nonatomic, weak) NSIndexPath * selectedIndex;
@property (nonatomic, weak)NSString * searchName;



DetailViewController.m

- (void)viewDidLoad {       //In new hardware _selectedIndex is OK, in some old hardware, is nil,  _searchName is always OK!
    [super viewDidLoad];
    Kings *flatkings = [Kings sharedKings];
    self.flatSequenceNumber = (int)[flatkings getFlatIndexFromTablePath:_selectedIndex];
    [self drawTheView];
}

【问题讨论】:

    标签: ios objective-c uiviewcontroller segue compatibility


    【解决方案1】:

    selectedIndex 和 searchName 的生命周期限定符应该是 copy,而不是 weak。两个视图控制器没有理由共享对同一字符串的引用。即使您想共享参考,您也应该使用strong,而不是weak,因为没有潜在的保留周期。

    Apple 的 Advanced Memory Management Programming Guide 是一个很好的资源,可以帮助您更好地了解生命周期限定符。

    【讨论】:

    • 好点!应该是副本。但是,我看不到 indexPath 是如何在某些硬件上变为 nil 的,而在 NSString 始终正确传递的情况下却没有。我已通过为 indexPath 添加全局引用来临时化,但想了解可能的潜在故障机制。
    • 您基本上已经在代码中编写了一个错误,该错误恰好在某些版本的 SDK 中起作用。
    • 不是很满意,但可能是真的;)
    猜你喜欢
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多