xcode Storyboard 使用 segue 将数据从视图控制器传递到标签栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12652334/
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
Storyboard pass data from view controller to tab bar with segue
提问by SiberKorsan
I have one page(say X) before tab bar controller that use model segue to open tab bar controller. Also first screen of tab bar controller is same with X.
我在标签栏控制器之前有一页(比如 X),它使用模型 segue 打开标签栏控制器。标签栏控制器的第一个屏幕也与 X 相同。
I want to pass data from X to tab bar controller's first page.
我想将数据从 X 传递到标签栏控制器的第一页。
Shortly, I want to pass data from view controller to tab bar controller page with storyboard segue. Is there any method for this ?
很快,我想将数据从视图控制器传递到带有故事板 segue 的标签栏控制器页面。有什么方法可以解决这个问题吗?
Here is the solution ;
这是解决方案;
locationsHome* vc = [[locationsHome alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];
回答by SiberKorsan
This is how I solved my problem. You can pass data from ViewController to TabBarController with this method.
这就是我解决我的问题的方法。您可以使用此方法将数据从 ViewController 传递到 TabBarController。
Use this code within prepareForSeguemethod
在prepareForSegue方法中使用此代码
locationsHome* vc = [[locationsHome alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];
Like this:
像这样:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString * segueIdentifier = [segue identifier];
if([segueIdentifier isEqualToString:@"tabbarGo"]){
locationsHome* vc = [[locationsHome alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];
etc...
}
}
回答by Ar No
//if sent to First Tab of Tab Bar Controller
UITabBarController *tabBarController = segue.destinationViewController;
UINavigationController *navigationController = (UINavigationController *)[[tabBarController viewControllers] objectAtIndex:0];
createTeamViewController *controller = (createTeamViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.userProfile = self.userProfile;
回答by swiftBoy
For Swift 2.2, I had to modify accepted answer this way
对于Swift 2.2,我不得不以这种方式修改接受的答案
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "userAccountSegue")
{
var firstTabViewController: UserAccountViewController = UserAccountViewController()
let tabViewController: UITabBarController = segue.destinationViewController as! UITabBarController
firstTabViewController = (tabViewController.customizableViewControllers![0] as! UserAccountViewController)
firstTabViewController.userObj = arrayForTable![selectedIndex] as? User
}
}
Note : Hint for above code snip
注意:提示上面的代码片段
- Useris my Modelobject storing user information
- My View controller segue is connected to the TabViewContoller in storyboard with segue = "userAccountSegue"
- 用户是我存储用户信息的模型对象
- 我的视图控制器 segue 连接到故事板中的 TabViewContoller, segue = " userAccountSegue"
回答by Rob
Add a property to the destination view controller (the subclassed tab bar controller) and then in the source view controller, implement prepareForSegueto pass information to the destination controller. You can then have the destination view controller's viewDidLoadto act upon the data that was passed to it in the prepareForSegueof the source view controller.
添加一个属性到目标视图控制器(子类标签栏控制器),然后在源视图控制器中,实现prepareForSegue将信息传递给目标控制器。然后,您可以让目标视图控制器对源视图控制器中viewDidLoad传递给它的数据进行操作prepareForSegue。

