从 Obj C didSelectionRow 调用 Swift ViewController [英] Call Swift ViewController from Obj C didSelectionRow

查看:23
本文介绍了从 Obj C didSelectionRow 调用 Swift ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Obj C 项目,我已经向其中添加了一个 Swift viewController.

I have an Obj C project that I have added a Swift viewController to.

我正在尝试将故事板附加到 viewController.

I'm trying to attach a storyboard to the viewController.

我可以以编程方式向视图添加内容,但无法显示故事板.

I can programmatically add things to the view but I can not get the storyboard to show up.

我也尝试过使用 xib 作为 Swift viewController 的竞争,但没有成功.

I have also tried to use a xib as a vie for the Swift viewController with no luck.

从 didSelectRowAtInexPath 调用 Swift vie form Obj C:

Calling the Swift vie form Obj C from a didSelectRowAtInexPath:

SwiftController *sc = [[SwiftController alloc] init];
[self.navigationController pushViewController:sc animated:YES];

SwiftController.swift:

SwiftController.swift:

import Foundation
import UIKit

@objc class SwiftController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

        // This prints
        println("in the scrolling view")
        // This works
        self.view.backgroundColor = .redColor();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

故事板已正确连接:

这就是我每次得到的:

同样的结果发生在 xib 上.

Same results happen with a xib.

Apple Docs 和网络上以及 SO 上有很多东西可以解决围绕"这个问题的许多问题,但我没有找到任何可以帮助我了解我在这里做错了什么的东西.以行之有效的方式做事或 Obj C 似乎行不通.显然,我是 Swift 的菜鸟.任何帮助都会很棒.谢谢.

There are a lot of things in the Apple Docs and on the web and here on SO that deal with many issues "around" this one but I have not found anything that helps me see what I'm doing wrong here. Doing things the way that works or Obj C does not seem to work. Clearly I'm a noob with Swift. Any help would be great. Thanks.

推荐答案

使用故事板有两种基本方法:

There are two basic approaches when using storyboards:

  1. 当您的 Objective-C 代码需要转换到故事板中的另一个场景时,您可以:

  1. When you have Objective-C code that needs to transition to another scene in the storyboard, you can:

  • 在 Interface Builder 中为该目标场景提供故事板 ID(例如 SwiftControllerScene);

使用故事板 instantiateViewControllerWithIdentifier 来实例化目标视图控制器;和

use the storyboard instantiateViewControllerWithIdentifier to instantiate that destination view controller; and

转换到新场景(即推送或呈现该视图控制器).

transition to that new scene (i.e. push or present that view controller).

因此:

SwiftController *sc = [self.storyboard instantiateViewControllerWithIdentifier:@"SwiftControllerScene"];
[self.navigationController pushViewController:sc animated:YES];

  • 或者,当使用故事板时,我宁愿在故事板中的视图控制器之间设置一个segue,而不是手动实例化目标的视图控制器然后调用pushViewController(请参阅https://stackoverflow.com/a/27650207/1271826).然后我会以编程方式执行转场(当然,参考您为转场提供的任何故事板标识符):

  • Alternatively, when using storyboards, rather than manually instantiating the destination's view controller and then calling pushViewController, I'd prefer to set up a segue in the storyboard between the view controllers (see https://stackoverflow.com/a/27650207/1271826). I'd then programmatically perform the segue (referencing, of course, whatever storyboard identifier you gave the segue):

    [self performSegueWithIdentifier:@"SegueToSwiftScene" sender:self];
    

    这样,故事板将继续代表应用的视觉流程.

    This way, the storyboard continues to represent the visual flow of the app.

    请注意,目标场景的视图控制器是用 Swift 与 Objective-C 编写的这一事实在很大程度上是无关紧要的.问题仅仅是源视图控制器使用什么语言(因此上面的示例是在 Objective-C 中的),并且无论目标视图控制器的语言如何,过程在其他方面基本相同.

    Note, the fact that the destination scene's view controller is written in Swift vs Objective-C is largely immaterial. The question is merely what language the source view controller uses (hence the above examples are in Objective-C) and the process is otherwise largely the same regardless of the language of the destination view controller.

    这篇关于从 Obj C didSelectionRow 调用 Swift ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆