【问题标题】:iOS/iPhone didSelectRowAtIndexPath self.navigationController pushViewController not workingiOS/iPhone didSelectRowAtIndexPath self.navigationController pushViewController 不工作
【发布时间】:2011-09-23 09:47:32
【问题描述】:

我肯定会失去理智。我已经编写了我认为是相当标准的用法 didSelectRowAtIndexPath: 方法在这里:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    if (row == 0){
        BuildingDescriptionViewController *buildingDescriptionViewController = [[BuildingDescriptionViewController alloc] initWithNibName:@"BuildingDescriptionViewController" bundle:[NSBundle mainBundle]];
        NSLog(@"self.navigationController0 is %@", self.navigationController ? @"not nil" : @"nil");
        [self.navigationController pushViewController:buildingDescriptionViewController animated:YES];
        [buildingDescriptionViewController release];
    }
    else if (row == 1){
        PostCommentViewController *buildingCommentViewController = [[PostCommentViewController alloc] init];
        NSLog(@"self.navigationController1 is %@", self.navigationController ? @"not nil" : @"nil");
        [self.navigationController pushViewController:buildingCommentViewController animated:YES];
        [buildingCommentViewController release];
    }
}

NSLog 通知 navigationController 不是 nil,但没有其他任何事情发生。 viewController 都没有出现。实际上,只是发生了 NSLog。我缺少一些深奥的用法吗?我也试过 [self presentModalViewController:buildingDescriptionViewController animated:YES];什么也没发生。我真的可以使用一些帮助。 谢谢。

更新: 下面根据 Sailesh 的建议输出

2011-06-27 18:02:56.370 something[16331:207] buildingdescriptionviewcontrollerinit 2011-06-27 18:02:56.370 东西[16331:207] 推送前的视图控制器:( “BuildingProfileViewController:0x7e89680” )

2011-06-27 18:02:56.370 something[16331:207] 推送后的视图控制器:( "BuildingProfileViewController: 0x7e89680>", “建筑描述视图控制器:0x7e73d30” )

2011-06-27 18:03:15.843 something[16331:207] buildingdescriptionviewcontrollerinit

2011-06-27 18:03:15.844 something[16331:207] 推送前的视图控制器:( "BuildingProfileViewController: 0x7e89680", “建筑描述视图控制器:0x7e73d30” )

2011-06-27 18:03:15.844 something[16331:207] 推送后的视图控制器:( “BuildingProfileViewController:0x7e89680” “建筑描述视图控制器:0x7e73d30” “建筑描述视图控制器:0xcc2c8e0” )

【问题讨论】:

  • 你确定 buildingDescriptionViewController/buildingCommentViewController 不是 nil 吗?
  • buildingDescriptionViewController 不是零,但即使它是不会有一些结果?令人困惑。
  • 可能我的情况不太清楚。当我选择行时,NSLog 会显示正确的行,但这是唯一的结果。没有其他事情发生。它不会爆炸。它只是不会推送到下一个 viewController。

标签: iphone objective-c ios cocoa-touch uinavigationcontroller


【解决方案1】:

检查视图控制器是否真的被推入导航堆栈。

要检查,请执行以下操作:

NSLog("ViewControllers before pushing: %@", self.navigationController.viewControllers);
[self.navigationController pushViewController:....];
NSLog("ViewControllers after pushing: %@", self.navigationController.viewControllers);

控制台中的输出会是viewControllers的一些地址,但是你至少会知道在push viewController之前和之后导航栈中viewControllers的数量。如果他们被推送,那么问题一定出在代码的其他部分,我想。

请回复输出。

【讨论】:

  • 谢谢赛莱什。他们确实被推了。
  • 我的意思是我在上面发布了输出。
  • 那么错误肯定在其他地方。检查是否正在为 BuildingDescriptionViewController 调用 viewDidLoad(以及类似的 viewWill.. 和 viewDid...)方法。您可能正在做一些使您的 rootViewController 成为活动 viewController 的事情。
【解决方案2】:

我刚刚通过调用任何视图方法或属性解决了我的问题。

childController = [[YouTubeLaunch alloc] initWithNibName:@"YouTubeView" bundle:nil];
        childController.view.userInteractionEnabled = YES;

过去 3 天我也遇到了同样的问题。虽然 viewController 正在添加,但只有 RootViewController 即将出现,而不是 YouTubeEmbedPlayer

2011-06-27 19:53:17.677 [1263:207] ViewControllers 推送前:( "", "", "", "") 2011-06-27 19:53:22.496 [1263: 207] 推送前的视图控制器:(“”,“”,“”,“”,“”)

【讨论】:

  • 谢谢拉克什。我试过了,我确实到了 viewDidLoad 但我仍然看不到视图。这很奇怪。我确实将控制器视图添加为子视图并且能够看到它,但这并不理想(当然)。我会坚持下去的。
【解决方案3】:

我终于找到了问题所在。我对 self.navigationController 不是 NIL 的事实感到困惑。问题是 tableView 已添加为子视图。我通过将它的 viewController 推到导航堆栈上来修复它。感谢所有回复的人。

【讨论】:

  • 我也有同样的问题。你能详细解释一下吗?我的 uiview 类中有三个子视图作为 uitableview。如果我单击表格单元格,我想更改屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多