-3

我创造了一个怪物,它让我感到困惑。

我创建了一个 rootViewController 来处理 iOS 应用程序视图和数据。这个控制器有一个 xib,它有助于它的外观。然后,我对自定义 UIView 类进行了子类化,并使用 xib 创建了它的视图。然后我尝试添加 UIView 类及其 xib 以显示在 RootViewControllers 视图中。所以 rootViewController 正在加载它的 NIb,我试图添加这个 UIView 子类并以编程方式安装它的视图。

我的文件层次结构如下..

mockAppDelegate.h
mockAppDelegate.m
mockViewController.h
mockViewController.m
mockViewController.xib
colorPickerLoad.h
colorPickerLoad.m
colorPickerLoad.xib

Code inside h controller file.

#import <UIKit/UIKit.h>
#import "colorPickerLoad.h"

@interface mockViewController : UIViewController
{

}

- (void)createColorPicker;

@end

In the controllers .m file:


@interface mockViewController ()

@end

@implementation mockViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
return self;
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   [self createColorPicker];

}

- (void)didReceiveMemoryWarning
{
[    super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)createColorPicker
{
    colorPickerLoad *newload = [[colorPickerLoad alloc] init];

   [self.view addSubview:newload];

   [self.view setNeedsDisplay];

}

@结尾

4

1 回答 1

0

当 rootViewController 正在加载它的 nib 时你不能这样做,但是你可以在加载完成之后并且在视图真正显示给用户之前实现它。

我真正的意思是在 rootViewController 实现中实现 viewDidLoad (如果它还不是自定义视图控制器,你就做一个):

- (void) viewDidLoad { 
    UINib *nib = [UINib nibWithNibName:@"yourSubViewNib" bundle:nil]; 
    // Change the owner with real owner of the nib, if it's not the controller. 
    // This shall return the list of top views in your nib 
    NSArray *views = [nib instantiateWithOwner:self options:nil]; 
}
于 2013-09-05T04:48:47.670 回答