I’m having a problem similar to this… Why am I seeing a black screen? Did I not alloc something?
Sorry for the beginner question but I’m not sure how to debug this. I’m seeing a blank (black) screen for one of my view controllers. When I set the view controller to “is initial view controller” in the inspector, it works as expected. However when I remove that (it shouldn’t be the initial VC, just did that to debug) and navigate to the view, I get a blank (black) screen.
There is a webview in the VC (referenceDetailWebView) and when I try to log that out on the viewDidLoad of the VC and I get (null). Again, when I set the VC to be “initial”, it logs out the referenceDetailWebView object as expected.
This is the implementation of the VC…
@implementation DetailViewController
@synthesize currentReference, referenceDetailWebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"reference/accommodation" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[referenceDetailWebView loadHTMLString:htmlString baseURL:nil];
// log for debugging
NSLog(@"%@", [self referenceDetailWebView]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
The only thing that I can think of is that I might be missing something in my segues. I’m handling them programmatically and it’s very possible that I’m overlooking something there…? My didSelectRowAtIndexPath looks like this…
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Reference *currentItem = [references objectAtIndex:indexPath.row];
NSString *mySegueID;
if([currentItem hasSubReferences]){
mySegueID = @"ShowSubTable";
} else {
mySegueID = @"ShowDetail";
}
[self performSegueWithIdentifier:mySegueID sender:nil];
}
And my performSegueWithIdentifier looks like this…
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
// the segue with identifier allows us to chose a segue programatically
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
if([identifier isEqualToString:@"ShowSubTable"]){
// looking for sub table
ReferencesSubTableViewController *subTableVC = [[ReferencesSubTableViewController alloc] initWithNibName:nil bundle:nil];
[subTableVC setCurrentReference:item];
[self.navigationController pushViewController:subTableVC animated:YES];
} else if ([identifier isEqualToString:@"ShowDetail"]){
// looking for detail view
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[self.navigationController pushViewController:detailVC animated:YES];
}
}
The segues are working as expected with the exception of that detailViewController displaying a blank screen and giving me null for my referenceDetailWebView.
I’m stuck. Any advice or guidance is very much appreciated! Thanks in advance for the help!!
Edit: It looks like the segues are probably the cause. When I remove the multiple segues and related code, and add a simple segue from the table cell to the DetailViewController only, the detailVC and views load correctly. Not sure what I’m missing in the segues though…?
I’ve simplified the performSegueWithIdentifier to the following and I’m still getting the blank screen…
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[detailVC setTitle:[item title]];
[self.navigationController pushViewController:detailVC animated:YES];
}
You’re getting a black screen because you’re instantiating your controller incorrectly. Also, you don’t push the view controller with pushViewController:animated:, that’s done for you when you call performSegueWithIdentifier:. If you have the segue setup in the storyboard, then you should be implementing prepareForSegue:sender:, not performSegueWithIdentifier:. Its code should look like this:
You really need to read and understand Apple’s docs on view controllers and segues — you clearly have some fundamental misunderstanding of the way they work.