简体   繁体   English

故事板使用segue将数据从视图控制器传递到标签栏

[英]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 注意:提示上面的代码片段

  • User is my Model object storing user information User是我的Model对象,用于存储用户信息
  • My View controller segue is connected to the TabViewContoller in storyboard with segue = " userAccountSegue " 我的View控制器segue连接到故事板中的TabViewContoller,其中segue =“ userAccountSegue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用segue将图像从视图控制器传递到选项卡栏? - how to pass image from view controller to tab bar with segue? 如何将图像视图从视图控制器传递到选项卡式视图控制器而无需保持链接 - How to pass image view from view controller to tab bar view controller without segue? 查看标签栏 controller 从 storyboard swift 5 - view tab bar controller from storyboard swift 5 使用storyboard segue iOS将数据传递给视图控制器 - pass data to view controller using storyboard segue iOS 标签栏控制器-如果从标签栏启动segue则更改数据 - Tab Bar Controller - change data if segue was initiated from tab bar 从Table View控制器到Tab Bar控制器的Segue - Segue from Table View controller to Tab Bar Controller 迅速,选择从标签栏控制器中拆分视图控制器 - Swift, segue to split View Controller from Tab Bar Controller XIB中的导航栏项目,显示了StoryBoard选项卡栏根视图控制器Segue - Nav Bar Item in XIB that Presents a StoryBoard Tab Bar Root View Controller Segue 故事板-从模态视图控制器返回到选项卡栏控制器 - Storyboard - go back to tab bar controller from modal view controller 在Storyboard xCode中从View Controller切换到Tab Bar Controller - Switch from View Controller to Tab Bar Controller in a Storyboard xCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM