Black screen on iPhone simulator


#1

Dear authors, i’ve checked the code many times and typed it absolutely the same way as it in the book, but it is just black screen on iPhone simulator

[code]// BNRAppDelegate.h
// iTahDoodle

#import <UIKit/UIKit.h>

//Declare a helper function that we will use to get a path
//to a location on disk where we can save the to-do list
NSString *docPath(void);

@interface ZBRAppDelegate : UIResponder <UIApplicationDelegate, UITableViewDataSource>
{
UITableView *taskTable;
UITextField *taskField;
UIButton *insertButton;

NSMutableArray *tasks;

}

-(void)addTask:(id)sender;

@property (strong, nonatomic) UIWindow *window;

@end
[/code]

[code]#import “ZBRAppDelegate.h”

NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@“data.td” ];
}

@implementation ZBRAppDelegate

@synthesize window = _window;

#pragma mark - Application delegate callbacks

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
    if (plist)
    {
    tasks = [plist mutableCopy];
    }
    else
    {
    tasks = [[NSMutableArray alloc] init];
    }

    if ([tasks count] == 0 ) {
    [tasks addObject:@“Walk the dog”];
    [tasks addObject:@“Feed the hogs”];
    [tasks addObject:@“Chop the logs”];
    }

    CGRect windowFrame = [[UIScreen mainScreen] bounds];
    UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
    [self setWindow:theWindow];

    CGRect tableFrame = CGRectMake(0, 80, 320, 380);
    CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
    CGRect buttonFrame = CGRectMake(228, 40, 72, 31);

    taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    [taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [taskTable setDataSource:self];

    taskField = [[UITextField alloc] initWithFrame:fieldFrame];
    [taskField setBorderStyle:UITextBorderStyleRoundedRect];
    [taskField setPlaceholder:@“Type a task, tap Insert”];

    insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [insertButton setFrame:buttonFrame];
    [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
    [insertButton setTitle:@“Insert” forState:UIControlStateNormal];

    [[self window] addSubview:taskTable];
    [[self window] addSubview:taskField];
    [[self window] addSubview:insertButton];

    [[self window] setBackgroundColor:[UIColor whiteColor]];
    [[self window] makeKeyAndVisible];

    return YES;

}

  • (void)addTask:(id)sender
    {
    NSString *t=[taskField text];

    if ([t isEqualToString:@""]) {
    return;
    }

    [tasks addObject:t];
    [taskTable reloadData];
    [taskField setText:@""];
    [taskField resignFirstResponder];

}

#pragma mark - Table View management

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [tasks count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
UITableViewCell *c= [taskTable dequeueReusableCellWithIdentifier:@“Cell”];

if (!c) {
    c= [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];

return c;

}

  • (void)applicationWillResignActive:(UIApplication )application
    {
    /

    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    */
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    [tasks writeToFile:docPath()
    atomically:YES];}

  • (void)applicationWillEnterForeground:(UIApplication )application
    {
    /

    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    */
    }

  • (void)applicationDidBecomeActive:(UIApplication )application
    {
    /

    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    */
    }

  • (void)applicationWillTerminate:(UIApplication *)application
    {
    [tasks writeToFile:docPath()
    atomically:YES];}

@end[/code]


#2

godimago,

Nothing looked immediately wrong with the code you posted, so I tried creating a test project and pasting your code in. I seems to be working fine on my machine.

One thing of note is that it can take the iOS Simulator a few moments to finish initializing - it sits on a black screen for a few seconds before the app launches. This is expected behavior for the iOS Simulator. Perhaps try running it again, and waiting a bit longer to see if the app launches as expected?

Here’s a link to the test project that I created using your code: ln.mw.am/GQBh

Let us know what kind of results you get.

~Mikey


#3

It works perfectly now! Thank you very much!