ios 如何以编程方式调用视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16134361/
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
How to call a View Controller programmatically?
提问by Samuel Noyes
I have looked at all the tutorials I can find on this one, and I still don't have the answer. I need to call another view from the code. I am using UIStoryboards
. I have changed the view many times by control-dragging from UIButtons
, but now it must be from the code. I am trying to call the info page from the main menu if it is the first time the user has opened the app. I cannot seem to find a way to change the views from the code, however. All my views are controlled by the same files (ViewController2). The identifier
of my main menu is ViewControllerMain, and the identifier
of the info page is ViewControllerInfo. First I tried this:
我已经查看了我可以找到的所有教程,但仍然没有答案。我需要从代码中调用另一个视图。我正在使用UIStoryboards
. 我通过控制拖动从 更改了多次视图UIButtons
,但现在它必须来自代码。如果这是用户第一次打开应用程序,我会尝试从主菜单调用信息页面。但是,我似乎找不到从代码中更改视图的方法。我所有的视图都由相同的文件(ViewController2)控制。在identifier
我的主菜单是ViewControllerMain,而identifier
该信息页面的是ViewControllerInfo。首先我试过这个:
[ViewControllerMain presentViewController: ViewControllerInfo
animated:YES
completion: NULL];
Then I tried making different UIViewControllers
for each and saying:
然后我尝试UIViewControllers
为每个人做不同的事情并说:
[ViewController2 presentViewController: ViewController
animated:YES
completion: NULL];
Neither worked. For the first one, it says:
都没有工作。对于第一个,它说:
Use of undeclared identifier ViewControllerMain.
使用未声明的标识符 ViewControllerMain。
In the second one, it says:
在第二个中,它说:
unexpected interface name 'ViewController': expected identifier.
意外的接口名称“ViewController”:预期标识符。
What can I do?
我能做什么?
回答by 190290000 Ruble Man
To create a view controller:
创建视图控制器:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
调用视图控制器(必须从另一个视图控制器中调用):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
一方面,使用 nil 而不是 null。
Loading a view controller from the storyboard:
从故事板加载视图控制器:
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier
of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
Identifier
您的视图控制器的名称等于您的视图控制器的类名,或者您可以在故事板的身份检查器中分配的故事板 ID。
回答by Suragch
Swift
迅速
This gets a view controller from the storyboard and presents it.
这从故事板中获取一个视图控制器并呈现它。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller name, and view controller id as appropriate.
根据需要更改故事板名称、视图控制器名称和视图控制器 ID。
回答by tigloo
You need to instantiate the view controller from the storyboard and then show it:
您需要从故事板实例化视图控制器,然后显示它:
ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.
此示例假设您有一个导航控制器以返回到上一个视图。你当然也可以使用presentViewController:animated:completion:。要点是让故事板使用目标视图控制器的 ID 实例化目标视图控制器。
回答by swiftBoy
You can call ViewController this way, If you want with NavigationController
你可以这样调用 ViewController,如果你想用 NavigationController
1.In current Screen : Load new screen
1.在当前屏幕:加载新屏幕
VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
2.1 In Loaded View : add below in .h file
2.1 在加载视图中:在 .h 文件中添加以下内容
@interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
2.2 In Loaded View : add below in .m file
2.2 在加载视图中:在 .m 文件中添加以下内容
@implementation VerifyExpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self;
[self setNavigationBar];
}
-(void)setNavigationBar
{
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}
-(void)onBackButtonTap:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
//todo for save button
}
@end
Hope this will be useful for someone there :)
希望这对那里的人有用:)
回答by Darren
There's 2 ways you can do this:
有两种方法可以做到这一点:
1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?
1,按照我在此处的回答中的说明,在您的故事板中为您的 ViewController 创建一个转场:如何在 iOS 5 中执行与用户输入无关的转场?
2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?
2,提供您的 ViewController 和标识符,并使用我在此处回答中的代码调用它:以编程方式调用故事板场景(无需转场)?
回答by Vaibhav Shiledar
main logic behind this is_,
这背后的主要逻辑是_,
NSString * storyboardIdentifier = @"SecondStoryBoard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];
[self presentViewController:UIVC animated:YES completion:nil];
回答by suresh
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil];
AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"];
// [self presentViewController:controller animated:YES completion:nil];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[topRootViewController presentViewController:controller animated:YES completion:nil];
回答by vijeesh
Import the view controller class which you want to show and use the following code
导入要显示的视图控制器类并使用以下代码
KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];