How to click UIViewController with NIB file

I'm new to iOS development, and I have a big headache, not understanding something that can be pretty easy. I mainly use the UITableViewController to switch to another UITableViewController, but then I need to switch from this second UITableViewController to the UIViewController. I want this UIViewController to have a .xib file for me to make a quick interface, but I cannot find a way to click from the second UITableViewController on this UIViewController. I was able to programmatically show the label, but not the user interface from .xib.

This is the code that works with the program label:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { self.detailController.title = @"Disclosure Button Pressed"; NSString *selectedMovie = self.movies[indexPath.row]; NSString *detailMessage = [[NSString alloc] initWithFormat:@"This is details for %@.", selectedMovie]; self.detailController.message = detailMessage; self.detailController.title = selectedMovie; [self.navigationController pushViewController:self.detailController animated:YES]; } 

And this is the -.m UIViewController, which works to programmatically display the UILabel.

 @implementation BIDDisclosureDetailViewController - (UILabel *)label; { return (id)self.view; } - (void)loadView; { UILabel *label = [[UILabel alloc] init]; label.numberOfLines = 0; label.textAlignment = NSTextAlignmentCenter; self.view = label; } - (void)viewWillAppear:(BOOL)animated; { [super viewWillAppear:animated]; self.label.text = self.message; } @end 

Basically, I don't want every UIObject to be software, I need .xib to create a user interface.

thanks

(iOS 6 without using storyboards)

+4
source share
2 answers

Downloading a UIViewController from xib is simple. When the tableview delegate sends the message didSelectRowAtIndexPath: you can load xib and present a view controller like this:

 BIDDisclosureDetailViewController *controller = [[BIDDisclosureDetailViewController alloc] initWithNibName:@"BIDDisclosureDetailView" bundle:nil]; [self.navigationController pushViewController:controller animated:YES]; 

Some notes:

  • You do not load the view controller from xib, but only from the view. Therefore, in the code above, I create a new instance of the view controller using initWithNibName:bundle:
  • In order for all this to connect correctly in IB, you set the "File Owner" in xib as the correct type of view controller (BIDDisclosureDetailViewController).
  • Be sure to remove the loadView code that you have there, this will get corrupted when you try to use xib.

Side Note! you should not have half-columns at the end of method signatures like this:

 - (void)viewWillAppear:(BOOL)animated; 

This is considered bad.

Hope this helps!

+13
source
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ViewControllerToBePushed *viewController = [[ViewControllerToBePushed alloc] initWithNibName:@"ViewControllerToBePushed" bundle:nil]; [self.navigationController pushViewController:viewController animated:YES]; } 

Use the above method to click to view the controller when a specific row is selected

0
source

All Articles