【问题标题】:Adding a TableViewController to an existing project将 TableViewController 添加到现有项目
【发布时间】:2011-07-09 21:14:34
【问题描述】:

我有一个现有的 TableViewController 如下

// TableViewController.h

@interface TableViewController : UITableViewController { NSArray *dataArray; }
@property (nonatomic, retain) NSArray *dataArray;

还有一个 navAppDelegate - 具体来说:

// navAppDelegate.h

#import <UIKit/UIKit.h>
@interface navwAppDelegate : NSObject 
<UIApplicationDelegate, UINavigationControllerDelegate> {
UIWindow *window;
IBOutlet UINavigationController *navigationController;}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;


// navAppDelegate.m
#import "navAppDelegate.h"

@implementation navigationtableviewAppDelegate
@synthesize window, navigationController;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{    
[window makeKeyAndVisible];
[window addSubview:[navigationController view]];
}

现在,我只是将文件添加到现有项目中,除了我将 (void)applicationDidFinishLaunching{} 的内容放在 (void)viewDidLoad{} 中,因为从现在开始它是视图而不是窗口(对吗?) .它不起作用,我的猜测是我必须将所有窗口调用从上面更改为视图?我在这里犯了什么根本错误? 我正在拨打电话

// StartOffHere.m

- (void)LetsGoButtonTouched {
navAppDelegate *newview = [[navAppDelegate alloc] initWithNibName:nil bundle:nil]; // I get a SIGABRT here
[[self navigationController] pushViewController: newview animated: YES];
}

【问题讨论】:

    标签: iphone xcode uiview sdk uiapplicationdelegate


    【解决方案1】:
    - (void)LetsGoButtonTouched {
    TableViewController *tableViewController = [[TableViewController alloc] init];
    [[self navigationController] pushViewController:tableViewController animated: YES];
    }
    

    试试看。这是你想要的吗?

    如果是这样,我所做的就是创建一个表视图控制器的新实例并推送它。在您的原始代码中,您试图推送无法完成的应用程序委托;应用程序委托不是视图控制器。不过,您的 UITableViewController 的子类 TableViewController 是这样的,因此您可以将其用于 pushViewController: 方法 - 并且表格视图将出现在屏幕上。

    【讨论】:

      【解决方案2】:

      谢谢本杰明。优秀的。但它并没有完全那样工作——经过两天的努力,我设法让它运行起来。对于那些感兴趣的人,这就是我所做的:

      1. 如果你想用 NavigationController 添加现有的 TableViewController,你只需要 TableViewController 和 DetailViewController 文件。忘记 AppDelegate。

      2. 执行两次新文件 -> 子类化并将现有代码分别复制到相同的 TableViewController .h/.m/.xib - 和 DetailViewController .h/.m/.xib 中。

      3. 当您进行方法调用时,您必须同时集成 TableViewController 和 NavigationController - 像这样:

        - (void)LetsGoButtonTouched {
        TableViewController *tvc = [[[TableViewController alloc] init] autorelease];
        UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:tvc]autorelease];
        [self presentModalViewController:navigationController animated:YES];
        

      顺便说一句,这个问题给了我提示: Add a UINavigationBar to a UITableViewController without a UINavigationController

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2020-12-22
        • 2013-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        相关资源
        最近更新 更多