0

好的,抱歉,简而言之:在多个视图控制器中重用从 nib 创建的视图的最佳/最简单/推荐的方法是什么?

4

1 回答 1

0

使用 的- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options方法NSBundle加载您的笔尖。

  • 创建一个 nib 文件并将文件的所有者设置为您的视图控制器之一。只要所有相关属性都存在于两者中,哪一个无关紧要。
  • 设置笔尖,链接您的视图/按钮/等。到文件的所有者属性。

在您的视图控制器中执行以下操作

@interface MyViewController : UIViewController {
}

@property (retain, nonatomic) IBOutlet UIView *myView;
// Add whatever other outlets you need for your nib.

@end

@implementation MyViewController

@synthesize myView;

- (void)viewDidLoad {
    [super viewDidLoad];

    // You set up your other views/ivars/etc. here

    [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
    // Assuming that your bundle contains a single top-level object that is linked to the
    //  'myView' property in your view controller, everything should be properly retained
}

@end
于 2011-01-11T12:42:53.443 回答