我创建了一个空的IOS项目,想要创建用户界面。我搜索了关于将storyboard连接到代码的信息,但是没有找到相关内容,只有关于xib文件的信息。那么在空项目中是否可以使用storyboard代替xib?如何实现?
我创建了一个空的IOS项目,想要创建用户界面。我搜索了关于将storyboard连接到代码的信息,但是没有找到相关内容,只有关于xib文件的信息。那么在空项目中是否可以使用storyboard代替xib?如何实现?
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 ];
注意:
首先创建一个空项目并给它起一个类名。
然后从文件->用户界面->故事板创建一个故事板。
接着为故事板命名,然后按左侧菜单中的故事板,在视图控制器中放置一个视图控制器,并将其保持为已创建的Uiviewcontrollerclass类名。
最后,选择项目并将主界面设置为ViewController.storyboard。
如果你想要编写代码,可以使用[self performSegueWithIdentifier: @"nameofcontroller" sender: self];
如果不想使用segue,可以这样做:
[self performSegueWithIdentifier: @"TheSequeName" sender: self]; 当你ctrl拖动一个按钮以打开一个新的视图控制器时,“TheSequeName”会被定义在故事板中。
如果你不使用segue,打开新的视图控制器还有许多变种。基本的方法是使用一个单独的NIB(没有故事板)。
SecondViewController *view = [[SecondViewController allow] initWithNibName:@"NibName" bundle:nil];
viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"viewStoryboardId"];
Then you show your viewController using navigation controller
[self.navigationController pushViewController:view animated:YES];
希望它能够正常工作
New File - > Objective-C Class (of type View Controller) -> without Xib file
现在在Storyboard上,转到那个特定的XIB文件
in property window -> Show Identity Inspector -> change class to above added ViewController.
就是这样,现在你可以通过代码控制你的故事板 XIB 文件了。
Storyboard
。Main.storyboard
。