[英]Storyboard pass data from view controller to tab bar with segue
I have one page(say X) before tab bar controller that use model segue to open tab bar controller. 在使用模型segue打开标签栏控制器的标签栏控制器之前我有一个页面(比如X)。 Also first screen of tab bar controller is same with X.
标签栏控制器的第一个屏幕与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. 不久,我想通过storyboard segue将数据从视图控制器传递到标签栏控制器页面。 Is there any method for this ?
这有什么方法吗?
Here is the solution ; 这是解决方案;
locationsHome* vc = [[locationsHome alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (locationsHome *)[[tbc customizableViewControllers] objectAtIndex:0];
This is how I solved my problem. 这就是我解决问题的方法。 You can pass data from ViewController to TabBarController with this method.
您可以使用此方法将数据从ViewController传递到TabBarController。
Use this code within prepareForSegue
method 在
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...
}
}
//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;
Add a property to the destination view controller (the subclassed tab bar controller) and then in the source view controller, implement prepareForSegue
to pass information to the destination controller. 将属性添加到目标视图控制器(子类标签栏控制器),然后在源视图控制器中,实现
prepareForSegue
以将信息传递到目标控制器。 You can then have the destination view controller's viewDidLoad
to act upon the data that was passed to it in the prepareForSegue
of the source view controller. 然后,您可以让目标视图控制器的
viewDidLoad
对源视图控制器的prepareForSegue
中传递给它的数据进行操作。
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 注意:提示上面的代码片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.