-1

I create two view. 1. ViewController by storyboard 2. A UIView by new a class. I want to use UIView class name: "ViewOne" to convert to a class, then load this UIView into the ViewController.

viewcontroller code:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.blue
    let anyobjectype : AnyObject.Type =      (NSClassFromString("TestIOSReflect.ViewOne"))!
    // TestIOSReflect is project name, ViewOne is the UIVIEW name
    let nsobjectype : NSObject.Type = anyobjectype as! NSObject.Type
    let rec: AnyObject = nsobjectype
    let currentView: UIView = rec as! UIView

    self.view.addSubview(currentView)

}

UIView: ViewOne code:

override init(frame: CGRect) {
    super.init(frame: frame)
    print("It's ViewOne")
    self.backgroundColor = UIColor.yellow
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
    print("It's draw func of ViewOne")
    //self.backgroundColor = UIColor.brown
}

result: Could not cast value of type 'TestIOSReflect.ViewOne' (0x109bc9570) to 'UIView' (0x10c2cdab0).

I retest after below suggestion: in ViewOne class: add " @objc(ViewOne) " in viewcontroller class: removed all below code:

self.view.backgroundColor = UIColor.blue
let anyobjectype : AnyObject.Type =           (NSClassFromString("TestIOSReflect.ViewOne"))!
// TestIOSReflect is project name, ViewOne is the UIVIEW name
let nsobjectype : NSObject.Type = anyobjectype as! NSObject.Type
let rec: AnyObject = nsobjectype
let currentView: UIView = rec as! UIView
self.view.addSubview(currentView)

Replaced by below code:

    if let viewOneClass = NSClassFromString("ViewOne") as? NSObject.Type{
        let ViewOne = viewOneClass.init()

        //self.view.addSubview(ViewOne)// doesn't work }

After run the project: in ViewOne class, printed out "It's ViewOne" ,but didn't run the code self.backgroundColor = UIColor.yellow

A.Sheh
  • 61
  • 8
  • 3
    This code smells very *objective-c-ish* – vadian Sep 27 '18 at 13:17
  • Are you sure it's not running that particular line? What I see here is that you have 2 views, one from the `UIViewController` and another that you're adding as a subview, **but** you're not setting the `frame` for that subview, so it doesn't have position and size. Did you try doing `currentView.frame = self.view.bounds` and **then** `self.view.addSubview(currentView)` – Alejandro Iván Sep 27 '18 at 13:30
  • I tried your code. fine error in this line: let anyobjectype : AnyObject.Type = (NSClassFromString("TestIOSReflect.ViewOne"))! The error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value – A.Sheh Sep 27 '18 at 14:17

1 Answers1

1

You can specify name for the symbol in Objective-C which omits the namespace in objective C by using the @objc annotation:

@objc(ViewOne)
class ViewOne: UIView {}

Then try to check if that class exists with the following code:

// Variable must first be casted into a specific type
if let viewOneClass = NSClassFromString("ViewOne") as? NSObject.Type {
  // Initialization
  let viewOne = viewOneClass.init()
  //// Do your stuff
  ....
}
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • Thanks, I followed your idea, and change by code, and the "init()" method run, and print "It's ViewOne", but didn't run code "self.backgroundColor = UIColor.yellow" – A.Sheh Sep 27 '18 at 13:18
  • Nice, glad it helped, would be cool if you accept my answer – Vlad Z. Oct 02 '18 at 18:30