xcode 如何将故事板连接到代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20839948/
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
How to connect storyboard to code
提问by Long Smith
I created empty IOS project and want to create UI. I searched for information about connecting storyboard to code but there is nothing about it, just about xib files. So is it possible to use storyboard instead xib in empty project? And how?
我创建了空的 IOS 项目并想创建 UI。我搜索了有关将故事板连接到代码的信息,但没有任何相关信息,仅与 xib 文件有关。那么是否可以在空项目中使用故事板代替 xib?如何?
回答by iPrabu
You can get the storyboard object like this assuming you have a Storyboard file already in the Project
假设您的项目中已经有一个故事板文件,您可以获得这样的故事板对象
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
YourViewController *yourVC = [storyBoard instantiateViewControllerWithIdentifier:@"identifier"];
//Do whatever with the viewcontroller object
[self.navigationController pushViewController:yourVC animated:YES ];
Note:
笔记:
- Check the Storyboard name
- Check your viewcontroller identifier in the Storyboard
- 检查故事板名称
- 检查故事板中的视图控制器标识符
回答by Yohan
First create an empty project with some class name
首先创建一个带有一些类名的空项目
then create an storyboard from file->userInterface->storyboard
然后从 file->userInterface->storyboard 创建一个storyboard
then give a name for the storyboard after that press storyboard in the left menu and place a viewcontroller and hold viewcontroller give class name as already created Uiviewcontrollerclass
然后在左侧菜单中按下故事板后为故事板命名并放置一个视图控制器并按住视图控制器给已经创建的类名 Uiviewcontrollerclass
finally press project choose main interface as ViewController.storyboard
最后按项目选择主界面为 ViewController.storyboard
if you wanna code suppose segue use [self performSegueWithIdentifier: @"nameofcontroller" sender: self];
如果你想编码假设 segue 使用 [self performSegueWithIdentifier: @"nameofcontroller" sender: self];
if don want segue go like this
如果不想继续这样
[self performSegueWithIdentifier: @"TheSequeName" sender: self]; The @"TheSequeName" is defined when you ctrl drag a button to open a new view controller on the storyboard
[self performSegueWithIdentifier: @"TheSequeName" sender: self]; @"TheSequeName" 在您按住 ctrl 拖动按钮以在情节提要上打开新视图控制器时定义
If you don't use segue, there are many variants to open a new view controller. The basic one is using a separate NIB (without storyboard)
如果你不使用 segue,有很多变体可以打开一个新的视图控制器。基本的是使用单独的 NIB(没有故事板)
SecondViewController *view = [[SecondViewController allow] initWithNibName:@"NibName" bundle:nil];
If you declare your view controller on Storyboard, you can use
如果你在 Storyboard 上声明你的视图控制器,你可以使用
viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"viewStoryboardId"];
Then you show your viewController using navigation controller
[self.navigationController pushViewController:view animated:YES];
Hope it works
希望它有效
回答by Sam Fischer
From the 'File' menu, create a new Storyboard
.
从“文件”菜单中,创建一个新的Storyboard
.
Call it Main.storyboard
.
叫它Main.storyboard
。
In your project settings, set it as the main interface file under the Deployment Info.
在您的项目设置中,将其设置为 Deployment Info 下的主界面文件。
回答by Shahab Qureshi
New File - > Objective-C Class (of type View Controller) -> without Xib file
now on on storyboard, go to that particular xib file
现在在故事板上,转到那个特定的 xib 文件
in property window -> Show Identity Inspector -> change class to above added ViewController.
That's it, now you can control your storyboard xib files from code.
就是这样,现在您可以通过代码控制您的故事板 xib 文件。