iOS 推送 ViewController 不工作

标签 ios uitableview pushviewcontroller

我已经阅读了很多类似的问题,但我无法弄清楚为什么 View 不起作用。我在这上面花了四个小时,所以我认为是时候寻求帮助了。

我的主要 VC 代码是一个 UITableView,在 didSelectRowAtIndexPath 中有这个方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __FUNCTION__);
    svc = [[SubViewController alloc] initWithNibName:@"SubView" bundle:nil];
    //UINavigationController *nc = [[UINavigationController alloc] init];

    switch (indexPath.section) {
    case 0 :
            switch (indexPath.row) {
            case 0 :
                    NSLog(@"0");
                    svc.label.text  = @"Item";
                    break;

                case 1:
                    NSLog(@"1");
                    svc.label.text  = @"Category";
                    break;
            }
            break;
    NSLog(@"2");        
    case 1 : svc.title  = @"Second Cell"; break;
    case 2 : svc.title = @"Third Cell"; break;
    case 3 : svc.title = @"Image"; break;
    case 4 : svc.title = @"Notes"; break;
            NSLog(@"3");
            break;
    }
    NSLog(@"4");
    //svc = [[SubViewController alloc] init];

    NSLog(@"svc is %@", svc);
    UINavigationController *nc = [[UINavigationController alloc] initWithNibName:@"SubView" bundle:nil];
                        //initWithRootViewController:svc];

    [nc pushViewController:svc animated:YES];

    NSLog(@"self.navigation is as %@",nc);
    NSLog(@"5");
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

您也可以看到我尝试过的内容。输出日志显示:

2013-02-12 13:32:22.858 CollapsableTableView[35122:c07] -[ViewController tableView:didSelectRowAtIndexPath:]
2013-02-12 13:32:22.859 CollapsableTableView[35122:c07] -[SubViewController initWithNibName:bundle:]
2013-02-12 13:32:22.860 CollapsableTableView[35122:c07] wtf
2013-02-12 13:32:22.860 CollapsableTableView[35122:c07] 0
2013-02-12 13:32:22.860 CollapsableTableView[35122:c07] 4
2013-02-12 13:32:22.861 CollapsableTableView[35122:c07] svc is <SubViewController: 0x10363070>
2013-02-12 13:32:22.863 CollapsableTableView[35122:c07] self.navigation is as <UINavigationController: 0x101608b0>
2013-02-12 13:32:22.863 CollapsableTableView[35122:c07] 5

因此正在找到 SubView,但未加载。任何想法都将受到重视。谢谢

更新。在接受评论后(谢谢 :))我的代码现在看起来像这样,但仍然是 nada...

svc = [[SubViewController alloc] initWithNibName:@"SubView" bundle:nil];


UINavigationController *nc = self.navigationController;


switch (indexPath.section) {
case 0 :
        switch (indexPath.row) {
        case 0 :
                NSLog(@"0");
                svc.label.text  = @"Item";
                break;

            case 1:
                NSLog(@"1");
                svc.label.text  = @"Category";
                break;
        }
        break;
NSLog(@"2");        
case 1 : svc.title  = @"Second Cell"; break;
case 2 : svc.title = @"Third Cell"; break;
case 3 : svc.title = @"Image"; break;
case 4 : svc.title = @"Notes"; break;
        NSLog(@"3");
        break;
}
NSLog(@"4");
//svc = [[SubViewController alloc] init];

NSLog(@"svc is %@", svc);

 [nc pushViewController:svc animated:YES];
//pushViewController:svc animated:YES];

NSLog(@"self.navigation is %@",nc);
NSLog(@"5");

最佳答案

在创建这个 View 之前添加一个导航 Controller (APP DELEGATE)或类似的。然后让这个 View 成为导航 Controller 的 Root View 。

最后。 self.navigationController pushViewController:(UIViewController*) animated:(BOOL) 我认为这可以解决问题。 如果您不确定如何添加导航 Controller ,请使用导航 Controller 的 Root View Controller 搜索 init。

但是如果你的意思是在这里添加一个新的 View Controller 那么你是正确的只是忘记将 subview 添加到主视图。

关于iOS 推送 ViewController 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839446/

相关文章:

json - 如何在 swift 中基于 Json id 从 Collection View didSelectItemAt 推送不同的 View Controller

ios - 在通话工具中屏蔽电话号码

ios - 插页式广告与原生广告

ios - 将 View 动画化到 collectionView 中的选定单元格或 scrollView 中的 tableView

ios - 如何从具有分页功能的 API 加载 UITableView 的数据?

iphone - 如何删除UITableView中最后一个单元格的最后一个边框?

iphone - 推送 View Controller 时无法设置 uiimageview 的图像

iphone - 没有标题动画的pushViewController

ios - SwiftUI subview 的角半径在父 View 也获取角半径时发生变化

ios - 如何对 View 中的约束进行分组