Call Swift ViewController from Obj C didSelectionRow
I have an Obj C project to which I have added a Swift viewController.
I am trying to attach a storyboard to a viewController.
I can programmatically add things to the view, but I cannot get the storyboard to appear.
I also tried using xib as vie for Swift viewController with no luck.
Calling Swift vie Obj C form from didSelectRowAtInexPath file:
SwiftController *sc = [[SwiftController alloc] init];
[self.navigationController pushViewController:sc animated:YES];
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.
}
}
The storyboard is correctly connected:
And this is what I get every time:
The same results occur with xib.
There are a lot of things in the Apple Docs and on the internet, and here on SO, that address a lot of issues around this, but I haven't found anything to help me figure out what I'm doing wrong here. Doing what works, or Obj C doesn't seem to work. Clearly I'm a nob with Swift. Any help would be great. Thank.
source to share
There are two main approaches when using storyboard:
-
If you have Objective-C code that needs to transition to a different scene in the storyboard, you can:
-
specify in the target string the storyboard ID (for example
SwiftControllerScene
) in Interface Builder; -
use a storyboard
instantiateViewControllerWithIdentifier
to instantiate this view controller; and -
transition to this new scene (i.e. click or present this view controller).
Thus:
SwiftController *sc = [self.storyboard instantiateViewControllerWithIdentifier:@"SwiftControllerScene"]; [self.navigationController pushViewController:sc animated:YES];
-
-
Alternatively, when using a storyboard rather than manually instantiating the view manager and then invoking it
pushViewController
, I would rather set up a segue in the storyboard between view controllers (see fooobar.com/questions/929949 / ... ). Then I did the segue programmatically (referencing, of course, whatever storyboard id you gave the segue):[self performSegueWithIdentifier:@"SegueToSwiftScene" sender:self];
In this way, the storyboard continues to represent the visual flow of the application.
Please note that the fact that the destination scene view controller is written in Swift vs Objective-C is pretty much irrelevant. The only question is what language the source view controller is in (hence the above examples are in Objective-C) and otherwise the process will be pretty much the same regardless of the target view controller language.
source to share
in case you are using xib for your quick viewController you should initialize it like this:
SwiftController* controller = [[SwiftController alloc] initWithNibName:@"SwiftController" bundle:nil]
Where SwiftController is also the name of your xib.
I notice this issue in iOS 8, but it works fine in iOS 9 and iOS 10 when you start the controller just like this:
SwiftController* controller = [[SwiftController alloc] init];
source to share