如何以编程方式调用View Controller?
我已经看过了我能找到的所有教程,但我仍然没有答案。我需要从代码中调用另一个视图。我正在使用UIStoryboards
。我已经通过控制 - 从UIButtons
多次改变了视图,但现在它必须来自代码。我试图从主菜单调用信息页面,如果这是用户第一次打开应用程序。但是,我似乎无法找到改变代码视图的方法。我的所有视图都由相同的文件控制(ViewController2)。我的主菜单中的identifier
是ViewControllerMain,信息页面的identifier
是ViewControllerInfo。首先,我想这:如何以编程方式调用View Controller?
[ViewControllerMain presentViewController: ViewControllerInfo
animated:YES
completion: NULL];
然后我试图使每个不同UIViewControllers
并说:
[ViewController2 presentViewController: ViewController
animated:YES
completion: NULL];
但是都没有成功。对于第一个,它说:
使用未声明的标识符ViewControllerMain。
在第二个,它说:
意外接口名称 '视图控制器':期望标识符。
我该怎么办?
要创建视图控制器:
UIViewController * vc = [[UIViewController alloc] init];
要调用的图控制器(必须从另一个视图控制器中调用):
[self presentViewController:vc animated:YES completion:nil];
其一,使用零而不是零。
从故事板加载视图控制器:
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
或者是等于你的视图控制器,或一个故事板ID的类名,你可以在身份分配你的故事板的检查员。
Hi @ 190290000 Ruble Man。这有效,但我想显示故事板文件中已经创建的视图。这只是给我一个空白的看法。有没有办法做到这一点?我正竭尽全力。我绝对有很多东西要学。 – 2013-04-21 19:53:28
谢谢@ 190290000卢布人!你最近的编辑工作! – 2013-04-22 00:35:12
@JohnFarkerson:没问题! – 2013-04-22 00:45:28
您需要实例从故事板视图控制器,然后显示它:
ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
这个例子假设你为了返回到先前的观点有一个导航控制器。你当然也可以使用presentViewController:animated:completion :.重点是让你的故事板使用目标视图控制器的ID实例化你的目标视图控制器。
有2种方法可以做到这一点:
1,创建你的故事板一个原因请看您的ViewController在我的答案在这里解释:How to perform a segue that is not related to user input in iOS 5?
2,给你的ViewController和标识,并使用叫它在我的答案在这里的代码:Call storyboard scene programmatically (without needing segue)?
可以调用视图控制器这种方式,如果你想与NavigationController
1.In当前屏幕:加载新的屏幕
VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
2.1加载视图:在.h文件中
@interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
2.2添加以下装货查看:下面添加in .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
希望这将是每个人都会有:)有用
导入要显示和使用下面的代码
KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];
斯威夫特
这从得到一个视图控制器视图控制器类故事板并呈现它。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
根据需要更改故事板名称,视图控制器名称和视图控制器ID。这确保对方背后
主要逻辑,
NSString * storyboardIdentifier = @"SecondStoryBoard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];
[self presentViewController:UIVC animated:YES completion:nil];
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];
这个问题是3岁的一个公认的答案,如果你有一个宝贵的贡献,请解释它,而不是只发布代码。 – pancho018 2016-11-11 10:27:59
如何创建控制器? – 2013-04-21 18:15:16
我在MainStoryboard.storyboard文件中创建了它们 – 2013-04-21 19:47:58