如何创建许多相似的 ViewController

How to create many similar ViewControllers

我想制作一个带有两个或三个按钮和标签的简单 ViewController。当我按下按钮时,它会播放一个音频文件。

我想制作数百个类似的屏幕,最好的制作方法是什么? (我目前正在使用 MainStoryBoard 创建它。)

我想对每个页面进行小的更改,例如按钮大小、按钮数量、标签文本和音频文件。

绘制文本气泡或使用 xib 文件可能不错,但我不确定我应该怎么做。

您首先需要拥有自己的 class 作为此 UIViewController 的基础 class,其中包含按钮、标签等

然后使用 Factory Design 模式生成继承的 UIViewController class,它允许您进行一些适合您需要的调整。

Factory Design Pattern

BaseViewController {
    UIButton *button1;
    UIButton *button1;
    UILabel *label1;
}

ChildViewControllerA : BaseViewController {
    UIButton *button3
}

ChildViewControllerB : BaseViewController {
    UIButton *button4
}

Factory : NSObject {
    + (BaseViewController)generateChildViewController: (int) type {
        switch (type)
            case 0:
                return [[ChildViewControllerA alloc] init];
            case 1:
                return [[ChildViewControllerB alloc] init];
     }
}

Main {
    - (void)createThreeViewControllers {
        BaseViewController *vc1 = [Factory generateChildViewController:0];
        vc1.button1.backgroundColor = [UIColor blueColor];
        BaseViewController *vc2 = [Factory generateChildViewController:0];
        vc2.button2.center = cgpointmake (100, 150);
        BaseViewController *vc3 = [Factory generateChildViewController:0];
        vc3.label1.text = @"vc3 test";
    }
}