简体   繁体   English

如何使用NIB文件推送UIViewController

[英]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. 我是iOS开发的新手,我很头疼,不理解可能很容易的东西。 Basically I'm using a UITableViewController to go to another UITableViewController, but then I need to go from that second UITableViewController to a UIViewController. 基本上我正在使用UITableViewController转到另一个UITableViewController,但后来我需要从第二个UITableViewController转到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. 我希望这个UIViewController有一个.xib文件供我快速创建UI,但我似乎无法找到从第二个UITableViewController推送到这个UIViewController的方法。 I managed to programatically show a label, but not a UI from a .xib. 我设法以编程方式显示标签,但不是来自.xib的UI。

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. 这是UIViewController的.m,它以编程方式显示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 to create any UIObject Programatically, I need a .xib to create the UI. 基本上我不想以编程方式创建任何UIObject,我需要一个.xib来创建UI。

Thanks 谢谢

(iOS 6, not using storyboards) (iOS 6,不使用故事板)

Loading up a UIViewController from a xib is easy. 从xib加载UIViewController很容易。 When your tableview delegate sends the didSelectRowAtIndexPath: message you can load up a xib and present the view controller as follows: 当您的tableview委托发送didSelectRowAtIndexPath:消息时,您可以加载xib并显示视图控制器,如下所示:

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. 您不从xib加载视图控制器,只加载视图。 Which is why in the code above I am creating a new instance of the view controller by using initWithNibName:bundle: 这就是为什么在上面的代码中我使用initWithNibName:bundle:创建一个新的视图控制器实例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). 为了使所有这些在IB中正确连接,您可以将xib中的“文件所有者”设置为正确的视图控制器类型(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. 确保删除你在那里的loadView代码,它会在尝试使用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! 希望这可以帮助!

- (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 push to view controller when a particular row is selected 使用上述方法在选择特定行时按下以查看控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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