2

In Objective-C

we can create class from string and initialize its object to push on navigation stack like :

Class myClass = NSClassFromString("ClassString");
id myObject = [[myClass alloc] initWithNibName:"ClassString" bundle:nil];
[self.navigationController pushViewController:myObject animated:YES];

What would be the equivalent code in swift ?

In Swift I've tried :

let ClassName = NSClassFromString("ClassString")
let myObject = ClassName.init(... ) // <- here I'm unable to init with nib name

enter image description here

How can the object be initialized with class created from NSClassFromString ?

UPDATE

With the help of Martin's link in comment, I'm able to fix problem with Nib/XIBs. But how it can be done for stoyboard ?

let mainStoryboard:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let myVC = mainStoryboard.instantiateViewController(withIdentifier: "SomeID") as ClassName // <- here I'm getting error
self.navigationController.pushViewController(myVC, animated: true)

enter image description here

UPDATE 2

I tried removing 'as ClassName' from the line and its working fine. As of now, pushing view controller using nib and storyboard is done. Now I want it to be push without both or with frame.

This code is just fine,but when it get pushed, it appeared as blank and black:

let myVC = ClassName.init()
self.navigationController?.pushViewController(myVC, animated: animate)

What is the solution ? If its init with frame, How can we achieve it in this case ?

All above conditions I'm checking because I want to implement one function for push/pop operation of viewcontroller.

Thanks!

iAkshay
  • 1,143
  • 1
  • 13
  • 35

0 Answers0