Tag: 指定初始化程序

使用故事板在UIViewController中初始化自定义的UIView

我有一个自定义的UIViewcontroller,并想初始化和分配一个自定义的UIView,我分配给一个IBOutlet。 我正在使用故事板。 任何人都可以给我提示在哪里调用自定义UIView的指定初始值设定项? **MyCustomUIView.h** @interface MyCustomUIView : UIView @end **MyCustomUIView.m** @implementation MyCustomUIView – (id)initWithNumberOfHeaderRows:(NSUInteger)headerRowCount numberOfHeaderColumns:(NSUInteger)headerColumnCount frame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } @end **MyUIViewController.h** @interface MyUIViewController : UIViewController @property (weak, nonatomic) IBOutlet MyCustomUIView *myCustomUIView; // I wanna use this Outlet @end **MyUIViewController.m** @implementation MyUIViewController @end 这是使用的github源的抽象版本: https : //github.com/mutualmobile/MMSpreadsheetView/blob/master/MMSpreadsheetView/MMSpreadsheetView.m

如何在Swift中编写UIViewController的init方法

我无法将init方法添加到以下UIViewController类。 我需要在init方法中编写一些代码。 我必须编写init(编码器)方法吗? 即使当我添加编码器和解码器的方法,我仍然得到错误。 我也尝试使用init方法,但没有任何参数,但似乎也没有工作。 class ViewController: UIViewController { var tap: UITapGestureRecognizer? override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nil, bundle: nil) tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) } … … } 如果我没有参数调用super.init()方法的错误是“必须调用超类的指定初始化”,如果我传递参数nib和捆绑,那么错误是“必需的初始化init(编码器)”。 即使当我添加init(编码器)和init(解码器),它不起作用。