简体   繁体   中英

UIViewController not loading a UIView

Hey, I'm playing around with a script my teacher provided for a table based application. However I can't seem to get my own view to load.

Files:

  • SubViewOneController (which is a sub view, also has a nib)
  • TapViewController (Custom UIView I created and want to add to a cell)
  • RootViewController (Main controller which loads in the views)
  • SimpleNavAppDelegate

How it works: Within the RootViewController, there's an NSArray that holds NSDictionary objects which is declared in the -(void)awakeFromNib {} method

- (void)awakeFromNib {

// we'll keep track of our views controllers in this array
views = [[NSMutableArray alloc] init];      // when using alloc you are responsible for it, and you will have to release it.

// ====================================================================================================
// ====================================================================================================

// LOADING IN CUSTOM VIEW HERE:

// allocate a set of views and add to our view array as a dictionary item
TapViewController *tapBoardView = [[TapViewController alloc] init]; 

//push onto array
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                  @"Tab Gestures",      @"title",
                  tapBoardView,         @"controller",
                  nil]];

[tapBoardView release]; //release the memory

// ====================================================================================================
// ====================================================================================================

SubViewOneController *subViewOneController = [[SubViewOneController alloc] init];

// This will set the 2nd level title
subViewOneController.title = @"Swipe Gestures"; //set it's title

//push it onto the array    
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                  @"Swipe Gestures",    @"title",
                  subViewOneController, @"controller",
                  nil]];

[subViewOneController release]; //release the memory

}

Later on I set the table view:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// OUTPUT -- see console
NSLog(@"indexPath %i", indexPath.row);  // OUTPUT: tapController: <TapViewController: 0x3b2b360>
NSLog(@"view object: %@", [views objectAtIndex:indexPath.row]); // OUTPUT: view object: controller = <TapViewController: 0x3b0e290>; title = "Tab Gestures";

// ----- Hardcoding the controller and nib file in does work, so it's not a linkage issue ------
// UNCOMMENT TO SEE WORKING -- comment below section.
//TapViewController *tapContoller = [[TapViewController alloc] initWithNibName:@"TapBoardView" bundle:nil];
//NSLog(@"tapController: %@", tapContoller); 
//[self.navigationController pushViewController:tapContoller animated:YES];

// ----- Random Tests -----
//UIViewController *targetViewController = [[views objectAtIndex: 0] objectForKey:@"controller"];       // DOES NOT WORK

// LOADS THE SECOND CELL (SubViewOneController) however will not load (TapViewController)
UIViewController *targetViewController = [[views objectAtIndex: indexPath.row] objectForKey:@"controller"];
NSLog(@"target: %@", targetViewController); // OUTPUT: target: <TapViewController: 0x3b0e290>

[self.navigationController pushViewController:targetViewController animated:YES];   

}

Reading the comments you should be able to see that hardcoding the view in, works - however trying to load it from the View NSArray does not work. It does however contain the object in memory, seeing that NSLog proves that.

Everything is linked up and working within the TapViewController nib file. So ya, im kinda stuck on this one, any help would be great!

Thanks guys

You're using the wrong initialization method for TapViewController at the top. A plain old init on a view controller isn't going to load a nib. And assuming your nib is hooked up properly (as you say), if you don't load the nib, then the view controller's view property never gets set to anything. So the view property is nil. So your view controller doesn't have any view to show when your app uses it.

TapViewController *tapBoardView = [[TapViewController alloc] init];

Whereas you're using the correct one in your "hardcoded" section:

TapViewController *tapContoller = [[TapViewController alloc] initWithNibName:@"TapBoardView" bundle:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM