AppDelegate和.xib没有正确实现,但构建成功了吗?

时间:2013-03-07 15:03:08

标签: ios xcode uitableview delegates xib

我真的不知道如何在不粘贴我的所有代码的情况下解释这一点,但生病了。 “假设”我的.hs和.ms是准确的,我感觉我的.xib设置不正确,但我真的不能粘贴代码。相反,我已压缩文件并上传源代码。 (如果你足够勇敢,它就在这里:http://bit.ly/ZtDkGi)我得到了一个成功的构建,但我的模拟器的屏幕在应用程序启动后只是黑屏。

基本上,我必须手动添加appDelegate对象。我将班级设置为适当的班级 - 但它仍然没有拉动。如果有人能够提供帮助,那就太好了。

这是我的Test_TableViewAppDelegate.h

#import <UIKit/UIKit.h>
@interface Test_TableViewAppDelegate : NSObject <UIApplicationDelegate>
{

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;

@end

这是我的 Test_TableViewAppDelegate.m

#import“Test_TableViewAppDelegate.h”

@implementation Test_TableViewAppDelegate

@synthesize window=_window;
@synthesize navController=_navController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];

UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor greenColor];
self.window = window;

UIViewController *fvc = [[UIViewController alloc] init];

UIViewController *rootController = [[UIViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];

//UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
self.navController = nc;

//[self.window addSubview: nc.view];
//[self.window makeKeyAndVisible];


self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
NSMutableArray *petsArray;

}

@end

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

最后但并非最不重要的是,main.m(我认为这也可能是一个问题)

#import "Test_TableViewAppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([Test_TableViewAppDelegate class]));
}
}
提前谢谢。我很感激:D

2 个答案:

答案 0 :(得分:1)

代理人Test_TableViewAppDelegate中的

为什么要向窗口添加两次视图?

// you could remove these two lines
[self.window addSubview: nc.view];
[self.window makeKeyAndVisible];  

//keep these two lines
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];

此视图是您添加到navigationController中的,它不会被任何nib名称

初始化
UIViewController *fvc = [[UIViewController alloc] init];

初始化应该在你的委托中

RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];

答案 1 :(得分:0)

我相信您获得黑屏的原因是您没有正确分配和初始化导航控制器!

相反,您应该尝试以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    // create the base window
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor greenColor];
    self.window = window;

    [window release];

    // this is the home page from the user's perspective

    FirstViewController *fvc = [[FirstViewController alloc] init];

    UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
    self.navigationController = nc;

    [fvc release];
    [nc release];

    // show them
    [self.window addSubview: nc.view];
    [self.window makeKeyAndVisible];


    return YES;
}

希望这有效!