How to get class name of parent ViewController in Swift?
Here's one approach:
if let parentVC = parent as? SomeViewController {
// parentVC is someViewController
} else if let parentVC = parent as? AnotherViewController {
// parentVC is anotherViewController
}
This will conditionally assign and unwrap parent
(the parent view controller) to its appropriate type. Within the condition blocks, parentVC
will be the correct class.
That said, this is a code smell - child view controllers should typically have no idea who their parent view controllers are. Whatever problem you're solving, you should probably solve it with tell, don't ask and delegation instead.
Get class name of Parent View controller in iPhone Navigation push
This should do it:
NSUInteger numberOfViewControllersOnStack = [self.navigationController.viewControllers count];
UIViewController *parentViewController = self.navigationController.viewControllers[numberOfViewControllersOnStack - 2];
Class parentVCClass = [parentViewController class];
NSString *className = NSStringFromClass(parentVCClass);
Get the parentViewController instance
The fact that it's difficult to get a reference to a view's associated controller should be a hint that it's bad practice. You would be better off passing the parent view controller into the view's initialiser and storing it as a weak reference.
class TheView: UIView {
weak var parentViewController: MyViewController?
init(parentViewController: MyViewController) {
self.parentViewController = parentViewController
}
}
// in MyViewController...
let view = TheView(parentViewController: self)
How to send values to a parent view controller in Swift
It is not a good idea to cast the parent view controller, even when you are sure which class represents. I'll do it with a protocol:
In the child controller add a protocol like:
protocol ChildNameDelegate {
func dataChanged(str: String)
}
class ChildClass {
weak var delegate: ChildNameDelegate?
func whereTheChangesAreMade(data: String) {
delegate?.dataChanged(data)
}
}
And in the parent:
class ParentClass: ChildNameDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let segueId = segue.identifier else { return }
switch segueId {
case "childSegue":
let destVC = segue.destinationViewController as! ChildClass
destVC.delegate = self
break
default:
break
}
}
// Child Delegate
func dataChanged(str: String) {
// Do whatever you need with the data
}
}
finding out superview's class name
I ended up setting a property, which does not answer the original question (sorry). There really should be a way though, even if I had done it by evaluating the frame of the super view.
Get child class from parent
You can use a function like this:
func getRawClassName(object: AnyClass) -> String {
let name = NSStringFromClass(object)
let components = name.componentsSeparatedByString(".")
return components.last ?? "Unknown"
}
which takes an instance of a class and obtain the type name using NSStringFromClass
.
But the type name includes the namespace, so to get rid of that it's split into an array, using the dot as separator - the actual class name is the last item of the returned array.
You can use it as follows:
class Parent {
class func sayHello() {
println("hi \(getRawClassName(self))")
}
}
and that will print the name of the actual inherited class
Related Topics
Difference Between Switch Cases "@Unknown Default" and "Default" in Swift 5
Combining Two Conditions in Nspredicate
Swift Check Type Against a Generic Type
Swiftui - Using Geometryreader Without Modifying the View Size
Swift 4.2+ Seeding a Random Number Generator
How to Set Title of Navigation Bar in Swift
Scene Kit Performance with Cube Test
Swift: How Can String.Join() Work Custom Types
Catch Objective-C Exception in Swift
Local Swift Packages Stopped Working in Xcode 13
Truncatingremainder VS Remainder in Swift
Xcode Autocomplete Does Not Work in Sources Folder of Swift Playgrounds
Synchronize Properties in Swift 3 Using Gcd
Insert, Update and Delete Animations with Foreach in Swiftui