Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Push an UIViewController with NIB file

I'm new to iOS development and I'm having a big headache not understanding something that may be pretty easy. Basically I'm using a UITableViewController to go to another UITableViewController, but then I need to go from that second UITableViewController to a UIViewController. I want this UIViewController to have a .xib file for me to make a quick UI but I can't seem to find the way to push from the Second UITableViewController to this UIViewController. I managed to programatically show a label, but not a UI from a .xib.

This is the code that works with the programmatic 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 of the UIViewController that works to show a UILabel programatically.

@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 to create any UIObject Programatically, I need a .xib to create the UI.

Thanks

(iOS 6, not using storyboards)

like image 788
Symphonicmind Avatar asked Dec 12 '22 16:12

Symphonicmind


1 Answers

Loading up a UIViewController from a xib is easy. When your tableview delegate sends the didSelectRowAtIndexPath: message you can load up a xib and present the view controller as follows:

BIDDisclosureDetailViewController *controller = [[BIDDisclosureDetailViewController alloc] initWithNibName:@"BIDDisclosureDetailView" bundle:nil];

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

Some things to note:

  1. You don't load up a view controller from a xib, only a view. Which is why in the code above I am creating a new instance of the view controller by using initWithNibName:bundle:
  2. In order for all of this to get hooked up properly in IB, you set the "File's Owner" in the xib to be of the correct view controller type (BIDDisclosureDetailViewController).
  3. Make sure to remove the loadView code you have in there as well, it will mess things up when trying to use the xib.

Side note! you should not have semi-colons at the end of method signatures like this one:

- (void)viewWillAppear:(BOOL)animated;

It is considered bad form.

Hope this helps!

like image 60
Dan Fairaizl Avatar answered Jan 11 '23 21:01

Dan Fairaizl