ios 以编程方式调用故事板场景(无需转场)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10522957/
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
Call storyboard scene programmatically (without needing segue)?
提问by zakdances
I have a modal storyboard scene that I want to be accessible to all my other scenes. Creating a modal segue to it from every scene on my storyboard creates a big mess of strings going everywhere. Is there a way that I leave off the segues and call the scene programmatically instead?
我有一个模态故事板场景,我希望所有其他场景都可以访问它。从我的故事板上的每个场景创建一个模态转场会产生一大堆到处都是的字符串。有没有一种方法可以让我放弃 segue 并以编程方式调用场景?
Basically I want to do something like this:
基本上我想做这样的事情:
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
[self presentModalViewController:myNewVC animated:YES];
except instead of creating and pushing a view controller class, I want to do a modal transition to an "isolated" (not connected with a segue) storyboard scene.
除了创建和推送视图控制器类之外,我想做一个模态转换到“隔离”(不与 segue 连接)故事板场景。
回答by Darren
Yes you can. Do something like this to get access to the VC, then just Modal Push it:
是的你可以。做这样的事情来访问 VC,然后只需 Modal Push:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"myViewCont"];
回答by Charles Wilson
Note: the method presentModalViewController:animatedis deprecated in iOS 6.
注意:方法presentModalViewController:animated在 iOS 6 中已弃用。
The new code should read:
新代码应为:
NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
回答by Jeremy1026
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code to bring that view forward.
在故事板中,为您的视图控制器提供一个标识符(在属性检查器下),然后使用以下代码将该视图向前推进。
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
回答by Alex Zavatone
I have a case where I want to present a view controller from the main part of the app, one with settings & help & so on. To do this, I want it to be within a nav controller, sort of a little plug in module we can call from a UIBarButtonItem.
我有一个案例,我想从应用程序的主要部分呈现一个视图控制器,一个带有设置和帮助等的视图控制器。为此,我希望它位于导航控制器中,有点像我们可以从 UIBarButtonItem 调用的小插件模块。
Now, this can be to/in the current storyboard, or to another, it doesn't matter.
现在,这可以是/在当前故事板中,也可以是另一个,这无关紧要。
I want to do it this way, because I loathe the potential of segue line spaghetti all over my storyboard.
我想这样做,因为我讨厌我的故事板上所有的segue line spaghetti的潜力。
Here's how to do it.
这是如何做到的。
- (IBAction)displaySettings:(id)sender
{
LOG_SELECTOR() // google that for extra goodness
// FYI, this can be done using a different storyboard like so.
/*
NSString * storyboardName = @"MainStoryboard_iPhone"; // possibly use device idiom?
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
*/
// To push a new set of scenes with a new Navigation Controller, it is done like this:
UINavigationController *settingsNC = [self.storyboard instantiateViewControllerWithIdentifier:@"Settings Nav Controller"];
OBSettingsUIViewController *settingsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Settings root"];
[settingsNC pushViewController:settingsVC animated:NO];
[settingsNC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Present the view controller;
[self presentViewController:settingsNC animated:YES completion:NULL];
}
In the presented view controllers (or in a subclass of the Navigation Controller), you can have a UIBarButtonItem to then dismiss the whole presented hierarchy of view controllers like so:
在呈现的视图控制器(或导航控制器的子类)中,您可以使用 UIBarButtonItem 来消除整个呈现的视图控制器层次结构,如下所示:
- (IBAction)dismissThisVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope this helps a bunch of people out. Cheers.
希望这可以帮助很多人。干杯。
回答by Kaushik Movaliya
Just call viewcontroller using navigation controller
Write this code in viewcontroller and set viewcontroller in storyboard as set in the image.
只需使用导航控制器调用viewcontroller 在viewcontroller 中编写此代码并在storyboard 中将viewcontroller 设置为图像中的设置。
ProfileVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ProfileVC"];
[self.navigationController pushViewController:vc animated:YES];
回答by Bhushan_pawar
Call to navigate to other class
调用导航到其他类
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UINavigationController *navController = (UINavigationController *)window.rootViewController;
DumpFeed *dump = [storyboard instantiateViewControllerWithIdentifier:@"DumpFeed"];
dump.isPushed=YES;
dump.strUserId = appDelegate.strFriendid;
[navController pushViewController:dump animated:YES];
回答by Esqarrouth
回答by supergegs
I think that with iOS7 it has become very easy implementing via the storyboard
我认为在 iOS7 中通过故事板实现它变得非常容易
I'm currently learning about the new features in iOS7 and found this simple solution, but it might have been relevant even in prior versions, I'm not sure.
我目前正在了解 iOS7 中的新功能并找到了这个简单的解决方案,但即使在以前的版本中它也可能是相关的,我不确定。
First u need to connect the presenting VC with the target VC (thats the only connection needed), then within the storyboard's attributes inspector choose the style to be modal, in the identity inspector give your VC a storyboardID and make sure you checked the 'use storyboardID' checkbox,
首先,您需要将呈现的 VC 与目标 VC 连接(这是唯一需要的连接),然后在故事板的属性检查器中选择要模态的样式,在身份检查器中为您的 VC 提供一个故事板 ID,并确保您选中了“使用故事板ID'复选框,
If its not there yet add this method to your presentingVC:
如果它还没有,请将此方法添加到您的presentingVC:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
YourTargetVC * targetVC =
(YourTargetVC *)segue.destinationViewController;
if(nil != targetVC) {
//Do preparations here
}
}
Now, when you wish to show your targetVC from your presentingVC you can use:
现在,当您希望从 PresentingVC 显示您的 targetVC 时,您可以使用:
[self performSegueWithIdentifier:(NSString *) sender:(id)];
where the identifier is your viewController's storyboardID, and the sender is the view who triggered the action, this method will invoke the storyboards scene, so the [prepareForSegue: sender:]
method will be called allowing u making last modifications before the targetViewController will appear.
其中标识符是您的 viewController 的 storyboardID,发送者是触发操作的视图,此方法将调用 storyboards 场景,因此将调用该方法,[prepareForSegue: sender:]
允许您在 targetViewController 出现之前进行最后修改。