从ObjC VC中删除presentVC.swift

时间:2014-11-02 22:54:38

标签: swift delegates presentmodalviewcontroller

情境:
主机VC Objective-C UIViewController(“BLSTimelineViewController”)。
提出的控制器是一个Swift UIViewController

我试图解雇 Swift VC

最初我尝试了以下内容:

标题链接:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "BLSTimelineViewController.h"

Objective-C 来源:

- (void) viewDidAppear:(BOOL)animated {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Bliss" bundle:nil];
    LoginViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"LoginVC"];
    controller.sendingVC = self;

    [self presentViewController:controller animated:NO completion:nil];

}

Swift Popup

...

import Foundation
import UIKit

class LoginViewController: UIViewController {

    var sendingVC:BLSTimelineViewController?

    // MARK: - Action functions

    @IBAction func loginAction(sender: UIButton) {

            sendingVC.dismissViewControllerAnimated(true, completion: nil)

    }

}

我收到以下编译器错误: enter image description here 这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

看起来您的代码无法编译 - 您是否真的将BLSTimelineViewController.h包含在ObjC-to-Swift桥接标题中?

更新:您的新错误是“BLSTimeViewController?没有名为...的成员”问号表示您不是在查看类的实际实例,而是查看包装器。要解开它,请添加!,例如:

sendingVC!.dismissViewControllerAnimated(...)

注意使用!你断言价值不是零 - 如果sendingVC恰好为零,你的代码就会崩溃。要处理它,请确保它不是零,或添加零检查。

请阅读此信息以了解有关展开的更多信息:https://mikeash.com/pyblog/friday-qa-2014-06-20-interesting-swift-features.html