我想用兩個或三個按鈕和標籤做一個簡單的ViewController。當我按下按鈕時,它播放一個音頻文件。如何創建許多類似的ViewControllers
我想製作數百個類似的屏幕,創建它的最佳方法是什麼? (我目前正在用MainStoryBoard創建它。)
對於每個頁面,我想對按鈕大小,按鈕數字,標籤文本和音頻文件進行小小更改。
繪製文本氣泡或使用xib文件可能是好的,但我不知道我該怎麼做。
我想用兩個或三個按鈕和標籤做一個簡單的ViewController。當我按下按鈕時,它播放一個音頻文件。如何創建許多類似的ViewControllers
我想製作數百個類似的屏幕,創建它的最佳方法是什麼? (我目前正在用MainStoryBoard創建它。)
對於每個頁面,我想對按鈕大小,按鈕數字,標籤文本和音頻文件進行小小更改。
繪製文本氣泡或使用xib文件可能是好的,但我不知道我該怎麼做。
首先,您需要有自己的這個的UIViewController爲具有按鈕,標籤基類類等
然後用工廠設計模式來生成繼承UIViewController類,它允許你做一些的調整,適合你需要。
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";
}
}
「你需要有你自己的這個UIViewController的類「這是否意味着我必須做的一切代碼?我目前正在使用MainStoryBoard設置按鈕,標籤等,這將是很難爲每個不同的ViewController .........編寫代碼/ – rebecca87
@ rebecca87作爲我寫的,你仍然需要創建一個基本視圖控制器類,你可以將其分配給故事板中的Viewcontroller。完成此操作後,您可以保存工作以在代碼中對其進行編輯,並且仍然需要調整這些元素以適應您以任何方式(例如Factory)創建的每個實例的代碼需求。在Storyboard中逐個創建ViewController的所有實例是很愚蠢的,特別是它們幾乎相同,可以通過繼承來解決。 –
um「保存編輯它們的工作,你仍然需要調整這些」這是否意味着我可以複製代碼並將其粘貼到新的ViewController?我只需要爲每個新頁面創建ViewController而不是在StoryBoard中創建它? – rebecca87
不知道你是問什麼 – sasquatch
爲什麼要進行數百頁? – RSon1234
使用單個故事板視圖控制器並創建它的多個實例... – Tj3n