ios - 在关闭中转到另一个 ViewController 不能快速工作

标签 ios swift uiviewcontroller

在执行了一些网络代码后(使用 AlamoFire,在闭包内),我试图转到另一个 ViewController(它是 TabBarController 的一部分)。我现在的 VC 的名字是 StartingVC。在我的闭包验证了一些代码之后,我想去另一个 VC。在我的示例中,代码得到满足,我 100% 确定,因为我有一个指示如此的 println(),但它不会将我带到另一个 VC。

我的关闭代码如下:

Alamofire.request(.POST, "http://localhost/test.php", parameters: dataToSend).responseJSON{
                (request, response, data, error) in

self.showViewController(HomeViewController(), sender: nil)

我知道我需要在闭包中使用 self 来引用我正在使用的实际 VC。但是代码不起作用。

你知道如何使用代码呈现/推送/转到另一个 VC 吗?我没有使用任何 Storyboard链接到我的 HomeViewController,因为在以编程方式和在 Storyboard中使用 segue 之前我遇到了一些问题,这就是我决定采用代码方法的原因。

提前谢谢你的帮助

干杯!

最佳答案

如果您不想使用 Storyboard设置导航 Controller 并且无法从那里开始,您的 appdelegate 应该是这样的:

class AppDelegate: UIResponder, UIApplicationDelegate {
//you must mark the window and navigationController variables as optional
var window: UIWindow?
var navigationController: UINavigationController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    navigationController = UINavigationController()
    var homeViewController: UIViewController = UIViewController()
    self.navigationController.pushViewController(homeViewController, animated: false)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    self.window.rootViewController = navigationController

    self.window.backgroundColor = UIColor.whiteColor()

    self.window.makeKeyAndVisible()

    return true
}
}

除了在 View Controller 上,您还可以将带有 Storyboard对象的 View Controller 推送为:

var firstViewController = self.storyboard!.instantiateViewControllerWithIdentifier("firstViewController") as! firstViewController
self.navigationController?.pushViewController(firstViewController, animated: false);

当前 View Controller :

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)

关于ios - 在关闭中转到另一个 ViewController 不能快速工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796459/

相关文章:

ios - 当应用程序处于非事件状态时,如何通过点击 UNUserNotification 操作按钮中的播放按钮来启动音频播放器?

ios - 如何在 iOS 中解析 html 以获取其所有内容以及标签名称?

ios - 为什么网络状态没有快速更新?

ios - 如何在设置页面检查我的应用程序是否禁用了 Face ID

ios - 使用 xcode 8 解散 vs 呈现

objective-c - AppDelegate 的当前 View Controller ?

ios - 无法在 IOS 中创建快照

ios - 如何使用 AWS Cognito 联合身份登录 Instagram

iphone - 子类化 UIWindow

json - 如果字典中有多个 json 字段同名,如何获取值?