【问题标题】:Getting "nested push animation can result in corrupted navigation bar"获取“嵌套推送动画可能导致导航栏损坏”
【发布时间】:2014-07-08 20:05:53
【问题描述】:

我完全被困住了。我一直在寻找答案,似乎每个人对每个“嵌套推送动画可能导致导航栏损坏”错误都有不同的问题。

请记住,我正在尝试自学如何为 iOS 7 编写代码。因此,如果我的某些编码方法不理想,我很抱歉,请提供反馈。无论如何,我正在创建一个 Pokemon 集换式卡牌游戏 Pokedex 应用程序,该应用程序显示最新系列的卡片。除了我选择主屏幕上的第一个表格单元格(XY Flash Fire)外,一切都运行得完美无缺。它将显示正确的表格数据,但导航栏标题不正确。当我选择一行时,它也不会进入 PokedexDetailViewController。

同样,主屏幕中的所有其他表格单元格都可以正常工作。我还尝试了人们在此处和 github 上发布的其他修复和课程,但没有一个对我有用。我还重新创建了整个 FlashFireViewController,但仍然遇到同样的问题。确保所有代码与其他工作视图控制器几乎相同。还验证了 segues 来自 Set View Controller,与单元格相反。然而,一旦它在 Flash Fire View Controller 中,segues 就来自单元格。

还不能发图片,所以这里是我的屏幕截图的专辑链接:http://imgur.com/a/Dq9py

  1. 我的故事板是如何设置的。
  2. 应用启动后的主屏幕。
  3. 当我选择第二个单元格 (XY) 时。
  4. 当我选择 Mega Venusaur EX 时。
  5. **错误。当我选择第一个单元格(XY Flash Fire)时。它显示了正确的表格数据,但不是正确的导航标题。
  6. 当我选择 Butterfree 或任何其他口袋妖怪时,什么都没有发生,并且导航标题消失了。此外,一旦我回击,应用就会崩溃并停止。

这里是 TCGSetViewController.m 的代码,它是主屏幕的类。

@interface TCGSetViewController ()

@end

@implementation TCGSetViewController{



 NSArray *thumbnailCell;
}

@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    thumbnailCell = [NSArray arrayWithObjects:@"XYFlashFire.png", @"XYBaseSet.png", @"BWLegendaryTreasures", @"BWPlasmaBlast.png", @"BWPlasmaFreeze.png", @"BWPlasmaStorm.png", nil];

}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [thumbnailCell count];


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}


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

    TCGSetCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    cell.setImage.image = [UIImage imageNamed:[thumbnailCell objectAtIndex:indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Conditionally perform segues, here is an example:
    if (indexPath.row == 0)
    {
        [self performSegueWithIdentifier:@"showFlashFireSet" sender:self];
    }
    if (indexPath.row == 1)
    {
        [self performSegueWithIdentifier:@"showXYBaseSet" sender:self];
    }
    else
    {
        [self performSegueWithIdentifier:@"showLegendarySet" sender:self];
    }
}

这里是 FlashFireViewController.m 的代码,它是第一个表格单元格。

@interface FlashFireViewController ()

@end

@implementation FlashFireViewController{

    NSArray *pokemons;
    NSArray *searchResults;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];


    Pokemon *caterpie = [Pokemon new];
    caterpie.name = @"Caterpie";
    caterpie.hitPoints = @"40 HP";
    caterpie.setNumber = @"1/106";
    caterpie.imageFile = @"1-caterpie.jpg";
    caterpie.rarity = @"COMMON";

    Pokemon *metapod = [Pokemon new];
    metapod.name = @"Metapod";
    metapod.hitPoints = @"70 HP";
    metapod.setNumber = @"2/106";
    metapod.imageFile = @"2-metapod.jpg";
    metapod.rarity = @"UNCOMMON";

    Pokemon *butterfree = [Pokemon new];
    butterfree.name = @"Butterfree";
    butterfree.hitPoints = @"130 HP";
    butterfree.setNumber = @"3/106";
    butterfree.imageFile = @"3-butterfree.jpg";
    butterfree.rarity = @"RARE";

    Pokemon *pineco = [Pokemon new];
    pineco.name = @"Pineco";
    pineco.hitPoints = @"60 HP";
    pineco.setNumber = @"4/106";
    pineco.imageFile = @"4-pineco.jpg";
    pineco.rarity = @"COMMON";

    Pokemon *seedot = [Pokemon new];
    seedot.name = @"Seedot";
    seedot.hitPoints = @"50 HP";
    seedot.setNumber = @"5/106";
    seedot.imageFile = @"5-seedot.jpg";
    seedot.rarity = @"COMMON";

    Pokemon *nuzleaf = [Pokemon new];
    nuzleaf.name = @"Nuzleaf";
    nuzleaf.hitPoints = @"80 HP";
    nuzleaf.setNumber = @"6/106";
    nuzleaf.imageFile = @"6-nuzleaf.jpg";
    nuzleaf.rarity = @"UNCOMMON";

    Pokemon *shiftry = [Pokemon new];
    shiftry.name = @"Shiftry";
    shiftry.hitPoints = @"140 HP";
    shiftry.setNumber = @"7/106";
    shiftry.imageFile = @"7-shiftry.jpg";
    shiftry.rarity = @"RARE";

    Pokemon *roselia = [Pokemon new];
    roselia.name = @"Roselia";
    roselia.hitPoints = @"60 HP";
    roselia.setNumber = @"8/106";
    roselia.imageFile = @"8-roselia.jpg";
    roselia.rarity = @"COMMON";

    Pokemon *roserade = [Pokemon new];
    roserade.name = @"Roserade";
    roserade.hitPoints = @"90 HP";
    roserade.setNumber = @"9/106";
    roserade.imageFile = @"9-roserade.jpg";
    roserade.rarity = @"UNCOMMON";

    Pokemon *maractus = [Pokemon new];
    maractus.name = @"Maractus";
    maractus.hitPoints = @"90 HP";
    maractus.setNumber = @"10/106";
    maractus.imageFile = @"10-maractus.jpg";
    maractus.rarity = @"UNCOMMON";

    pokemons = [NSArray arrayWithObjects:caterpie, metapod, butterfree, pineco, seedot, nuzleaf, shiftry, roselia, roserade, maractus, nil];

}

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

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    } else {
        return [pokemons count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *pokemonTableIdentifier = @"PokemonTableCell";

    TCGPokedexCell *cell = (TCGPokedexCell *)[self.tableView dequeueReusableCellWithIdentifier:pokemonTableIdentifier];

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


    Pokemon *pokemon = [pokemons objectAtIndex:indexPath.row];
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        pokemon = [searchResults objectAtIndex:indexPath.row];
    } else {
        pokemon = [pokemons objectAtIndex:indexPath.row];
    }
    cell.pokemonLabel.text = pokemon.name;
    cell.pokemonNum.text = pokemon.setNumber;
    cell.thumbnailImageView.image = [UIImage imageNamed:pokemon.imageFile];
    cell.pokemonHP.text = pokemon.hitPoints;
    cell.pokemonRarity.text = pokemon.rarity;

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"showPokedexDetail"]) {
        NSIndexPath *indexPath = nil;
        Pokemon *pokemon = nil;

        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            pokemon = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            pokemon = [pokemons objectAtIndex:indexPath.row];
        }

        PokedexDetailViewController *destViewController = segue.destinationViewController;
        destViewController.pokemon = pokemon;
    }
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [pokemons filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}



@end

这是我得到的确切错误代码:

2014-07-08 12:38:18.409 TCG Pokedex[37666:60b] nested push animation can result in corrupted navigation bar
2014-07-08 12:38:18.849 TCG Pokedex[37666:60b] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
2014-07-08 12:38:22.222 TCG Pokedex[37666:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
*** First throw call stack:

【问题讨论】:

  • 我看不出你的代码有什么问题,假设 TCGSetViewController 中的 segues 都是由你所说的控制器制成的。当您尝试同时执行两次推送时,通常会出现此错误,例如代码中的一次 segue 和一次手动推送,但我在您的代码中没有看到类似的内容。

标签: ios iphone objective-c xcode


【解决方案1】:

您的问题很可能出现在tableView:didSelectRowAtIndexPath 中的TCGSetViewController.m

您有一个if 语句,后跟一个if else 语句。当indexPath.row == 0 第一个if 为真时,执行[self performSegueWithIdentifier:@"showFlashFireSet" sender:self]。但随后代码执行继续到if else 块。那里有两个单独的执行块。由于indexPath.row 仍然为0,我们将进入else 块,并执行另一个segue。

您很可能希望将其结构化为

if (indexPath.row == 0)
{
    [self performSegueWithIdentifier:@"showFlashFireSet" sender:self];
}
else if (indexPath.row == 1) // notice the else on this line
{
    [self performSegueWithIdentifier:@"showXYBaseSet" sender:self];
}
else
{
    [self performSegueWithIdentifier:@"showLegendarySet" sender:self];
}

更好的是,把它放在 switch 语句中:

NSString *segueID = nil;
switch (indexPath.row) {
    case 0:
        segueID = @"showFlashFireSet";
        break;
    case 1:
        segueID = @"showXYBaseSet";
        break;
    default:
        segueID = @"showLegendarySet";
        break;
}
[self performSegueWithIdentifier:segueID sender:self];

P.S.: 更多 cmets

从 iOS 5 或 6 或 7 或其他版本开始,dequeueReusableCellWithIdentifier:forIndexPath: 保证返回有效单元格,因此在调用该方法后无需检查 cell == nil(假设您不是针对旧 iOS 版本) .

我看到你只是打电话给dequeueReusableCellWithIdentifier:,它确实有可能返回nil。但是您在发布的代码中调用了该方法两次,并且两次都在 tableView 数据源方法中调用,该方法有一个 indexPath 可供使用,因此请利用它并删除那些 if (cell == nil) 检查;这是解放。

【讨论】:

  • 感谢您解决问题。我现在完全明白了。我忘记了所有关于开关的事情。我知道有更好的方法来选择多个转场。
【解决方案2】:

试试这个,我自己没有测试过代码,但它可以工作。

这里是问题区域。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    // You got a problem with all these ifs and elses. 2 things are happening. 
    if (indexPath.row == 0) <----Is called
    {
        [self performSegueWithIdentifier:@"showFlashFireSet" sender:self];

    }
    if (indexPath.row == 1) <---also called
    {
        [self performSegueWithIdentifier:@"showXYBaseSet" sender:self];
    }
    else <--- this is also called. 
    {
        [self performSegueWithIdentifier:@"showLegendarySet" sender:self];
    } */



    if (indexPath.row == 0)
{
    [self performSegueWithIdentifier:@"showFlashFireSet" sender:self];
     break;
}
else if (indexPath.row == 1) // notice the else on this line
{
    [self performSegueWithIdentifier:@"showXYBaseSet" sender:self];
    break;
}
else
{
    [self performSegueWithIdentifier:@"showLegendarySet" sender:self];
     break;
}

}

【讨论】:

  • 感谢您的意见。我尝试了您的代码,但“中断”会引发错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 2013-11-22
相关资源
最近更新 更多