How to Access Both Objective-C and Swift Classes from Same Storyboard

How to access both Objective-C and Swift classes from same storyboard?

It seems that Xcode 6.0 (6A215l) needs to upgrade the Storyboard file to a newer version before it can access Swift classes.

If you replaced an Objective-C class with a Swift class with the same name, you need to open the Storyboard, edit the name of the custom class (like remove the last letter), save the Storyboard and then rename the class back. This will force the Storyboard file to be upgraded to a newer version, and the new Xcode to correctly write the custom class key like this:

Version information diff:
Sample Image

UIButton custom class definition diff:
Sample Image

After these changes in the Storyboard file the warning seems to disappear and the correct custom class is used.

Can a UI class with different Obj-C and Swift names be used in Storyboard?

You said it yourself: you have exposed a different name to Objective-C. So the class's name is PrefixCustomButton as far as Interface Builder is concerned.

Using an objective-C class as Custom Class in storyboard in swift project

I have downloaded the github code and tried using bridging header. It works as expected. Below are the screenshots & code which I added in bridging header file. Hope you did same steps but may be missed something.

//
// SwiftL1-Bridging-Header.h
// SwiftL1
//
// Created by uttamkumar_s on 4/13/15.
// Copyright (c) 2015 Uttam. All rights reserved.
//

#ifndef SwiftL1_SwiftL1_Bridging_Header_h
#define SwiftL1_SwiftL1_Bridging_Header_h

#import "SLPagingViewController.h"

#endif

Sample Image

Sample Image

Sample Image

Open view Controller of Swift from Objective C class

I initially faced same trouble but after searching on web I got success. you should follow exact same as I have mentioned below:

Step by step Swift integration for Xcode Objc-based project:

Create new *.swift file (in Xcode) or add it by using Finder
Create an Objective-C bridging header when Xcode ask you about that
Implement your Swift class with @objc attribute:

import UIKit

@objc public class CustomView: UIView {
override func draw(_ rect: CGRect) {
// Drawing code
}
}

Open Build Settings and check those parameters:

  • Defines Module : YES

  • Product Module Name : myproject

Make sure that your Product Module Name doesn't contain any special
characters

  • Install Objective-C Compatibility Header : YES

Once you've added *.swift file to the project this property will
appear in Build Settings

  • Objective-C Generated Interface Header : myproject-Swift.h

This header is auto-generated by Xcode

  • Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h

Import Swift interface header in your *.m file

#import "myproject-Swift.h"

Don't pay attention to errors and warnings.
Clean and rebuild your Xcode project.
Done!

Swift, How can I connect 2 classes to 1 scene in storyboard?

Let's say your ButtonHandler looks something like this:

class ButtonHandler: NSObject {
@IBOutlet weak var button: UIButton!

override init() {
super.init()
}

@IBAction func buttonTapped(sender: AnyObject) {
print("button tapped")
}
}

In your storyboard, search for "object" component panel panel (bottom right of screen):

Search for object

Drag that component to the scene panel (left in storyboard). Select the object from the scene panel and change the class of the object in the top, right panel:

Change the object class

You can now connect the button outlets to those of the ButtonHandler.

You can also create an outlet to a ButtonHandler in your UIViewController and access the handler from code.

How to use single storyboard uiviewcontroller for multiple subclass

great question - but unfortunately only a lame answer. I don't believe that it is currently possible to do what you propose because there are no initializers in UIStoryboard that allow overriding the view controller associated with the storyboard as defined in the object details in the storyboard on initialization. It's at initialization that all the UI elements in the stoaryboard are linked up to their properties in the view controller.

It will by default initialize with the view controller that is specified in the storyboard definition.

If you are trying to gain reuse of UI elements you created in the storyboard, they still must be linked or associated to properties in which ever view controller is using them for them to be able to "tell" the view controller about events.

It's not that much of a big deal copying over a storyboard layout especially if you only need a similar design for 3 views, however if you do, you must make sure that all the previous associations are cleared, or it will get crashes when it tries to communicate to the previous view controller. You will be able to recognize them as KVO error messages in the log output.

A couple of approaches you could take:

  • store the UI elements in a UIView - in a xib file and instantiate it from your base class and add it as a sub view in the main view, typically self.view. Then you would simply use the storyboard layout with basically blank view controllers holding their place in the storyboard but with the correct view controller sub class assigned to them. Since they would inherit from the base, they would get that view.

  • create the layout in code and install it from your base view controller. Obviously this approach defeats the purpose of using the storyboard, but may be the way to go in your case. If you have other parts of the app that would benefit from the storyboard approach, it's ok to deviate here and there if appropriate. In this case, like above, you would just use bank view controllers with your subclass assigned and let the base view controller install the UI.

It would be nice if Apple came up with a way to do what you propose, but the issue of having the graphic elements pre-linked with the controller subclass would still be an issue.

have a great New Year!!
be well



Related Topics



Leave a reply



Submit