我对iOS开发相当陌生。我通常在代码中创建我所有的 UIViews、UILabels、UIButtons 等,而不是使用 Interface Builder。这使得我的 UIViewControllers 变得非常大且难以遵循,将插座声明与实际操作和逻辑混合在一起非常容易。
- (UIButton *) continueButton {
// if button is nil
if(_continueButton == nil) {
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
/* more configuration */
_continueButton = button;
}
return _continueButton;
}
// occures when continue button is tapped
- (void) buttonTouch:(id) sender
{
UIButton *button = ((UIButton *)sender);
if(button == continueButton) {
/* do stuff */
}
}
我想要完成的是将实际动作、动画、逻辑等分开,并将按钮、标签和其他视图的声明存储在不同的文件中。
作为一种解决方案,我正在考虑使用“addChildViewController”创建另一个带有插座的 UIViewController,并将其作为子节点嵌入到包含逻辑的主节点中。
这是正确的方法吗?你如何处理它?