ios 将故事板添加到现有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8973286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding storyboard to existing project
提问by glebreutov
I have created a project without support of storyboards, but later I have added a storyboard. Storyboard work well and i see it's contents when loading app in emulator.
我创建了一个不支持故事板的项目,但后来我添加了一个故事板。Storyboard 运行良好,我在模拟器中加载应用程序时看到它的内容。
Then I created a view controller (UIViewController), and my problem is I can't bind any control from storyboard to my controller.
然后我创建了一个视图控制器(UIViewController),我的问题是我无法将故事板中的任何控件绑定到我的控制器。
So my question is how to connect my controller with storyboard.
所以我的问题是如何将我的控制器与故事板连接起来。
回答by OSXMonk
Those exact steps (I am using XCode 4.5 and iOS 6.0 ):
那些确切的步骤(我使用的是 XCode 4.5 和 iOS 6.0 ):
- Add new storyboard to the project by File->New->File...->Userinterface->storyboard
- Go to project summary and select MainStoryboard and select the storyboard name you just created.
- From AppDelegate.m file in method didFinishLaunchingWithOptions, comment everything except last statement which returns YES.
- 通过 File->New->File...->Userinterface->storyboard 向项目添加新的故事板
- 转到项目摘要并选择 MainStoryboard 并选择您刚刚创建的故事板名称。
- 在方法 didFinishLaunchingWithOptions 中的 AppDelegate.m 文件中,注释除最后一条返回 YES 的语句之外的所有内容。
That is all. Now you should see your Initial view as you launch the project.
就这些。现在您应该在启动项目时看到您的初始视图。
回答by Raz
For XCode 6 and iOS8 beta2:
对于 XCode 6 和 iOS8 beta2:
- Create a Storyboard file (let's call it Main.storyboard). Pick an iPhone or iPad for device type.
- In the blank Main.storyboard file, drag a UIViewController from the object library.
- If the storyboard is intended to be used as a universal storyboard, then switch to the File Inspector (in the Utilities section on the right), and check mark "Use Size Classes".
- Select the View Controller in the storyboard and switch to the Identity Inspector (in the Utilities section on the right), and type in the "Class" field the name of your View Controller.
- Find the file "info.plist" in the "Supporting Files" folder on the left in the Navigator section.
- Add a row by clicking on the '+' sign to the right of the "information property list" key.
- Select "Main storyboard file base name" key, and type the name of your storyboard file, in the "Value" field (in this case it will be called "Main")
- In AppDelegate.m/AppDelegate.swift, in the method/func "application:didFinishLaunchingWithOptions:" remove all code except "return YES" (for Objective-C), or "return true" for Swift.
- 创建一个 Storyboard 文件(我们称之为 Main.storyboard)。选择 iPhone 或 iPad 作为设备类型。
- 在空白的 Main.storyboard 文件中,从对象库中拖出一个 UIViewController。
- 如果故事板打算用作通用故事板,则切换到文件检查器(在右侧的实用程序部分),并选中“使用大小类”。
- 在 storyboard 中选择 View Controller 并切换到 Identity Inspector(在右侧的 Utilities 部分),然后在“Class”字段中输入您的 View Controller 的名称。
- 在导航器部分左侧的“支持文件”文件夹中找到文件“info.plist”。
- 单击“信息属性列表”键右侧的“+”号添加一行。
- 选择“主故事板文件基本名称”键,然后在“值”字段中键入故事板文件的名称(在本例中将称为“主”)
- 在 AppDelegate.m/AppDelegate.swift 中,在方法/函数“application:didFinishLaunchingWithOptions:”中删除除“return YES”(对于 Objective-C)或“return true”之外的所有代码(对于 Swift)。
回答by glebreutov
I've found answer on my own question. It's very simple: I should open storyboard, select view controller (it's visual representation on storyboard), then go to identity inspector and replace class UIViewController to your controller class
我在我自己的问题上找到了答案。这很简单:我应该打开故事板,选择视图控制器(它是故事板上的视觉表示),然后转到身份检查器并将 UIViewController 类替换为您的控制器类
回答by konturgestaltung
Have you checked if the controls you want to connect have an corresponding IBOutlet or IBAction in the controller? Then you should be able to connect them as usual.
您是否检查过您要连接的控件在控制器中是否有相应的 IBOutlet 或 IBAction?然后你应该能够像往常一样连接它们。
回答by Alexander
Add Main.storyboard
Add row in Info.plist > Main storyboard file base name:Main
Edit code:
添加 Main.storyboard
在 Info.plist > Main storyboard file base name:Main 中添加行
编辑代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Override point for customization after application launch. return YES; }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //应用启动后覆盖自定义点。返回是;}
回答by Valeriy Kliuk
Don't forget to check the main.m:
不要忘记检查main.m:
#import <UIKit/UIKit.h>
#import "YourAppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegate class]));
}
}