添加TableViewController到现有项目

问题描述:

我有现成的TableViewController如下添加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 {}中,因为它从现在起是一个视图,而不是一个窗口(right?)。它不起作用,我的猜测是我必须将所有窗口调用从上方更改为视图?我在这里犯了什么根本错误? 我从

// StartOffHere.m 

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

- (void)LetsGoButtonTouched { 
TableViewController *tableViewController = [[TableViewController alloc] init]; 
[[self navigationController] pushViewController:tableViewController animated: YES]; 
} 

拨打电话尝试。这就是你想要发生的事情吗?

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

谢谢本杰明。优秀。但它并没有那样工作 - 经过两天的努力,我设法运行。对于那些谁是interesed,这里就是我所做的:

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

  2. 执行两次新文件 - > Subclass,并分别将现有代码复制到相同的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