I have a nifty project I downloaded from GitHub (here) and I am playing around with it. The project has no storyboard or xibs whatsoever, and only one viewController, which is defined with just a viewController.h file and a viewController.m file.
我有一个从GitHub(这里)下载的漂亮项目,我正在玩它。该项目没有任何storyboard或xibs,只有一个viewController,它只使用viewController.h文件和viewController.m文件定义。
Perhaps a noob question, but can I have viewController1.h/m programmatically segue to viewController2.h/m without using ANY xibs or storyboards? I found a lot of code on SO and elsewhere allowing one to segue programmatically from one view to another within a Storyboard, from one xib to another or from a scoreboard to a xib (though not the opposite) but nothing on how to segue from one totally code-based vc to another. All the code I found requires that you define the view in terms of the bundle location of the storyboard or xib file, but I want to use neither.
也许是一个菜鸟问题,但我能不能使用任何xib或故事板以编程方式将viewController1.h / m转换为viewController2.h / m?我在SO和其他地方发现了很多代码,允许一个人在一个故事板中以编程方式从一个视图到另一个视图,从一个xib到另一个或从记分板到xib(虽然不是相反),但没有关于如何从一个完全基于代码的vc到另一个。我发现的所有代码都要求您根据storyboard或xib文件的bundle位置定义视图,但我不想使用它们。
Note: I accepted the answer I did because of its ingenuity/interesting-ness, but for the sake of simplicity I personally ended up opting with this answer to the same question (mine was a duplicate it appears): iOS: present view controller programmaticallly
注意:我接受了我所做的答案,因为它的独创性/趣味性,但为了简单起见,我个人最终选择了同一个问题的答案(我看来是重复的):iOS:目前的视图控制器programmaticallly
2 个解决方案
#1
You can use [viewController presentViewController:anotherController animated:YES completion:nil];
to present the view controller modally.
你可以使用[viewController presentViewController:anotherController animated:YES completion:nil];以模态方式呈现视图控制器。
Another alternative is to use a UINavigationController
and do [viewController.navigationController pushViewController:anotherController animated:YES];
另一种方法是使用UINavigationController并执行[viewController.navigationController pushViewController:anotherController animated:YES];
The second method will only work if viewController
is in the stack of a navigationController
第二种方法仅在viewController位于navigationController的堆栈中时才有效
#2
Here is my Context class which changes view controllers. It works with either your own view classes or storyboard view classes.
这是我的Context类,它改变了视图控制器。它适用于您自己的视图类或故事板视图类。
Specific to your question look at the open function. If there is no root controller when I call open, I assign it as the root view controller. Otherwise I present it from the root view controller.
具体到你的问题看看open函数。如果在调用open时没有根控制器,我将其指定为根视图控制器。否则我从根视图控制器呈现它。
import Foundation
import UIKit
private let _StoryBoard = UIStoryboard(name: "Main", bundle: nil)
private let _RootWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
public var ROOT_VIEW_CONTROLLER:UIViewController = C_RootViewController()
//abstract base of context classes
class Context:NSObject
{
class var STORYBOARD:UIStoryboard
{
return _StoryBoard
}
class var ROOTWINDOW:UIWindow
{
return _RootWindow
}
var _currentController:Controller!
class func reassignRootViewController(controller:UIViewController)
{
Context.ROOTWINDOW.rootViewController = controller
ROOT_VIEW_CONTROLLER = controller
}
func initController(controllerName:String)->Controller
{
return Context.STORYBOARD.instantiateViewControllerWithIdentifier(controllerName) as Controller
}
func initControllerFromStoryboard(storyboardName:String,controllerName:String)->Controller
{
var storyboard:UIStoryboard = UIStoryboard(name: storyboardName, bundle: nil)
return storyboard.instantiateViewControllerWithIdentifier(controllerName) as Controller
}
func open(controller:UIViewController)
{
if(Context.ROOTWINDOW.rootViewController == nil)
{
Context.ROOTWINDOW.rootViewController = ROOT_VIEW_CONTROLLER
Context.ROOTWINDOW.makeKeyAndVisible()
}
ROOT_VIEW_CONTROLLER.presentViewController(controller, animated: true, completion: {})
}
func close(controller:UIViewController)
{
ROOT_VIEW_CONTROLLER.dismissViewControllerAnimated(true, completion: nil)
}
}
#1
You can use [viewController presentViewController:anotherController animated:YES completion:nil];
to present the view controller modally.
你可以使用[viewController presentViewController:anotherController animated:YES completion:nil];以模态方式呈现视图控制器。
Another alternative is to use a UINavigationController
and do [viewController.navigationController pushViewController:anotherController animated:YES];
另一种方法是使用UINavigationController并执行[viewController.navigationController pushViewController:anotherController animated:YES];
The second method will only work if viewController
is in the stack of a navigationController
第二种方法仅在viewController位于navigationController的堆栈中时才有效
#2
Here is my Context class which changes view controllers. It works with either your own view classes or storyboard view classes.
这是我的Context类,它改变了视图控制器。它适用于您自己的视图类或故事板视图类。
Specific to your question look at the open function. If there is no root controller when I call open, I assign it as the root view controller. Otherwise I present it from the root view controller.
具体到你的问题看看open函数。如果在调用open时没有根控制器,我将其指定为根视图控制器。否则我从根视图控制器呈现它。
import Foundation
import UIKit
private let _StoryBoard = UIStoryboard(name: "Main", bundle: nil)
private let _RootWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
public var ROOT_VIEW_CONTROLLER:UIViewController = C_RootViewController()
//abstract base of context classes
class Context:NSObject
{
class var STORYBOARD:UIStoryboard
{
return _StoryBoard
}
class var ROOTWINDOW:UIWindow
{
return _RootWindow
}
var _currentController:Controller!
class func reassignRootViewController(controller:UIViewController)
{
Context.ROOTWINDOW.rootViewController = controller
ROOT_VIEW_CONTROLLER = controller
}
func initController(controllerName:String)->Controller
{
return Context.STORYBOARD.instantiateViewControllerWithIdentifier(controllerName) as Controller
}
func initControllerFromStoryboard(storyboardName:String,controllerName:String)->Controller
{
var storyboard:UIStoryboard = UIStoryboard(name: storyboardName, bundle: nil)
return storyboard.instantiateViewControllerWithIdentifier(controllerName) as Controller
}
func open(controller:UIViewController)
{
if(Context.ROOTWINDOW.rootViewController == nil)
{
Context.ROOTWINDOW.rootViewController = ROOT_VIEW_CONTROLLER
Context.ROOTWINDOW.makeKeyAndVisible()
}
ROOT_VIEW_CONTROLLER.presentViewController(controller, animated: true, completion: {})
}
func close(controller:UIViewController)
{
ROOT_VIEW_CONTROLLER.dismissViewControllerAnimated(true, completion: nil)
}
}