简体   繁体   English

从Storyboard获取ViewController

[英]Get ViewController from Storyboard

I have two ViewControllers, which aren`t (directly) connected with a Segue. 我有两个ViewControllers,它们没有(直接)与Segue连接。 But I want to change a String in the second VC, which i get in the first one. 但我想在第二个VC中更改一个字符串,我在第一个中获取。 My try was: 我的尝试是:

    #import "secondViewController.h"

    @interface firstViewController ()
    @property NSString* originalString;
    @end

    @implementation firstViewController

    -(void)viewDidDisappear:(BOOL)animated{
        [super viewDidDisappear:animated];
        secondViewController* svc = [secondViewController new];
        svc.anotherString = self.originalString;    
    }

But it dosent work, because I've only created a instance of the second VC, so the value was not saved. 但它起作用,因为我只创建了第二个VC的实例,因此没有保存该值。 Also I can`t use the Storyboard ID, because I use Xcode 5. 我也不能使用Storyboard ID,因为我使用的是Xcode 5。

I have a menuVC from which you can get to the firstVC and the secondVC. 我有一个menuVC,你可以从中获得firstVC和secondVC。 And from the firstVC I can go back (with the navigationbackbarbutton) to the menu. 从第一个VC我可以返回(使用navigationbackbarbutton)到菜单。 so: menu->firstVC->menu. 所以:menu-> firstVC-> menu。 menu->secondVC->...->menu 菜单 - > secondVC - > ...->菜单

My try with StoryboardID: 我尝试使用StoryboardID:

    -(void)viewDidDisappear:(BOOL)animated{
        [super viewDidDisappear:animated];

        secondViewController* svc =[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"secondVCSrorybradID"];

        svc.anotherString = self.originalString;    
    }

you can pass the string to second view controller with this code. 您可以使用此代码将字符串传递给第二个视图控制器。

secondViewController* svc =[self.storyboard instantiateViewControllerWithIdentifier:@"Your Second VC's Storyboad ID"];
svc.anotherString = self.originalString;    
[self presentViewController:svc animated:YES completion:nil];

//you have to create anotherString property in second View Controller's .h File. //你必须在第二个View Controller的.h文件中创建anotherString属性。

now you can get the string of originalString to second VC. 现在你可以将originalString的字符串输入到第二个VC。 Now you can get this value back second VC to first VC. 现在你可以将这个值从第二个VC恢复到第一个VC。

hope this helps you. 希望这能帮助你。

You should pass your data successively through your UIViewController s navigation. 您应该通过UIViewController的导航连续传递数据。 If, for example, you have a navigation like FirstVC > SecondVC > ThirdVC : 例如,如果您有类似FirstVC > SecondVC > ThirdVC的导航:

In your FirstVC.m, use : 在FirstVC.m中,使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    ((SecondVCClass*) segue.destinationViewController).secondVCString = _firstVCString;
}

With secondVCString being a @property in your second ViewController. 随着secondVCString是一个@property在你的第二个视图控制器。

In your SecondVC.m, use : 在SecondVC.m中,使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    ((ThirdVCClass*) segue.destinationViewController).thirdVCString = _secondVCString;
}

With of course thirdVCString being a @property in your third ViewController. 当然了thirdVCString是一个@property在你的第三个视图控制器。


Edit: 编辑:

As you updated your question, here is what I suggest : 当您更新问题时,我建议:

  • In your MenuVC, add this : 在您的MenuVC中,添加以下内容:

     @property (nonatomic, weak) NSString *importantString; 
  • In your FirstVC and SecondVC, add this : 在FirstVC和SecondVC中,添加以下内容:

     @property (nonatomic, weak) MenuVCClass *menu; 
  • When you push to FirstVC or SecondVC, use prepareForSegue to set the destination's view controller menu property to your menu : 当您按下FirstVC或SecondVC时,请使用prepareForSegue将目标的视图控制器menu属性设置为您的菜单:

     -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"FirstSegue"]) ((FirstVC*) segue.destinationViewController).menu = self; else if ([segue.identifier isEqualToString:@"SecondSegue"]) ((SecondVC*) segue.destinationViewController).menu = self; } 
  • In FirstVC or SecondVC, you can change the NSString value from your menu using _menu.importantString = @""; 在FirstVC或SecondVC中,您可以使用_menu.importantString = @"";从菜单中更改NSString_menu.importantString = @"";

You should never use new to create a view controller. 您永远不应该使用new来创建视图控制器。

If you're using storyboards but not using segues, you can still create a view controller from the storyboard and invoke it. 如果您正在使用故事板但不使用segue,您仍然可以从故事板创建视图控制器并调用它。

Use the method instantiateViewControllerWithIdentifier: to create an instance of the target view controller. 使用方法instantiateViewControllerWithIdentifier:创建目标视图控制器的实例。 The set the properties you want to set, and finally make a call to display it (present it modally, push it onto the navigation stack, or whatever is appropriate for your program.) 设置要设置的属性,最后调用以显示它(以模态方式显示,将其推送到导航堆栈,或适合您的程序的任何内容。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM