ios 从 Storyboard 获取 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22178017/
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
Get ViewController from Storyboard
提问by Sam
I have two ViewControllers, which aren`t (directly) connected with a Segue. But I want to change a String in the second VC, which i get in the first one. My try was:
我有两个 ViewController,它们不(直接)与 Segue 连接。但是我想更改第二个 VC 中的字符串,这是我在第一个 VC 中获得的。我的尝试是:
#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. Also I can`t use the Storyboard ID, because I use Xcode 5.
但它不起作用,因为我只创建了第二个 VC 的实例,所以没有保存该值。我也不能使用 Storyboard ID,因为我使用的是 Xcode 5。
I have a menuVC from which you can get to the firstVC and the secondVC. And from the firstVC I can go back (with the navigationbackbarbutton) to the menu. so: menu->firstVC->menu. menu->secondVC->...->menu
我有一个 menuVC,您可以从中访问第一个 VC 和第二个 VC。从 firstVC 我可以返回(使用导航返回栏按钮)到菜单。所以:菜单->firstVC->菜单。菜单->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;
}
采纳答案by rdurand
You should pass your data successively through your UIViewController
s navigation. If, for example, you have a navigation like FirstVC > SecondVC > ThirdVC
:
您应该通过您UIViewController
的导航连续传递您的数据。例如,如果您有如下导航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
在你的第三个 ViewController 中。
Edit:
编辑:
As you updated your question, here is what I suggest :
当你更新你的问题时,这是我的建议:
In your MenuVC, add this :
@property (nonatomic, weak) NSString *importantString;
In your FirstVC and SecondVC, add this :
@property (nonatomic, weak) MenuVCClass *menu;
When you push to FirstVC or SecondVC, use
prepareForSegue
to set the destination's view controllermenu
property to your 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 = @"";
在您的 MenuVC 中,添加以下内容:
@property (nonatomic, weak) NSString *importantString;
在您的 FirstVC 和 SecondVC 中,添加以下内容:
@property (nonatomic, weak) MenuVCClass *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; }
在 FirstVC 或 SecondVC 中,您可以
NSString
使用从菜单中更改值_menu.importantString = @"";
回答by
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.
//您必须在第二个视图控制器的 .h 文件中创建 anotherString 属性。
now you can get the string of originalString to second VC. Now you can get this value back second VC to first VC.
现在您可以将 originalString 字符串获取到第二个 VC。现在您可以将该值从第二个 VC 返回到第一个 VC。
hope this helps you.
希望这对你有帮助。
回答by Duncan C
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.
如果您使用的是故事板但不使用转场,您仍然可以从故事板创建一个视图控制器并调用它。
Use the method instantiateViewControllerWithIdentifier:
to create an instance of the target view controller. 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.)
使用该方法instantiateViewControllerWithIdentifier:
创建目标视图控制器的实例。设置您要设置的属性,最后调用以显示它(以模态方式呈现,将其推送到导航堆栈上,或者任何适合您的程序的内容。)