AP.*_*AP. 1 iphone uiviewcontroller uiview
我有一个以编程方式创建的自定义UIview.如何将自定义UIViewController与其关联(以编程方式)
感谢致敬,
loadView在UIViewController没有nib文件的情况下以编程方式实现创建视图层次结构.
- (void)loadView {
// allocate the subclassed UIView, and set it as the UIViewController's main view
self.view = [[[UIViewSubclass alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
}
Run Code Online (Sandbox Code Playgroud)
您可以通过两种方式继续设置视图/子视图层次结构.一种是在自定义UIView的初始化方法中添加它们,如下所示:
// in the MyView.m file
- (id)initWithFrame:(CGRect)f {
if (self = [super initWithFrame:f]) {
// add subviews here
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是继续使用子类中loadView实现的方法UIViewController,并使用[self.view addSubview:anotherView].(或者,使用中的viewDidLoad方法UIViewController subclass.)
注意:替换initWithFrame:使用自定义UIView的初始化方法(例如,initWithDelegate:).