Get All List of Uiviewcontrollers in iOS Swift

Get all list of UIViewControllers in iOS Swift

You can do it with below code.

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

if let viewControllers = appDelegate.window?.rootViewController?.presentedViewController
{
// Array of all viewcontroller even after presented
}
else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers
{
// Array of all viewcontroller after push
}

Swift 4.2 ( XCode 10 )

let appDelegate = UIApplication.shared.delegate as! AppDelegate

if (appDelegate.window?.rootViewController?.presentedViewController) != nil
{
// Array of all viewcontroller even after presented
}
else if (appDelegate.window?.rootViewController?.children) != nil
{
// Array of all viewcontroller after push
}

How to list all live (non-deallocated) UIViewControllers?

Run app in debugger and use "Debug memory graph" button debug memory graph and see the list of the view controllers in the panel on the left. If you happened to follow the convention of including ViewController in the name of your view controllers (e.g. MainViewController, DetailsViewController, etc.), you can filter the list of objects listed in the left panel by typing ViewController in the "filter" text box at the bottom of the left panel:

Sample Image

In this example, I also clicked on my third view controller, and I can see it was presented by the second one, which was presented by the first one.


The other approach is to use the "view debugger" Sample Image, but that only shows the view controllers that are currently present in the active view controller hierarchy and may not show view controllers whose views are not currently visible because the view controller presented another view controller modally.

How to list out all the subviews in a uiviewcontroller in iOS?

You have to recursively iterate the sub views.

- (void)listSubviewsOfView:(UIView *)view {

// Get the subviews of the view
NSArray *subviews = [view subviews];

for (UIView *subview in subviews) {

// Do what you want to do with the subview
NSLog(@"%@", subview);

// List the subviews of subview
[self listSubviewsOfView:subview];
}
}

iOS (Swift) - Array of UIViewControllers

You need instances

private var array: [AViewController] = [BViewController(), CViewController(), DViewController()]

if the vcs are in IB , then you would do

let b = self.storyboard!.instantiateViewController(withIdentifier: "bID") as! BViewController
let c = self.storyboard!.instantiateViewController(withIdentifier: "cID") as! CViewController
let d = self.storyboard!.instantiateViewController(withIdentifier: "dID") as! DViewController

Then

array = [b,c,d]

How to find the calling viewController in my stack?

Every view controller has either a parent or a presentingViewController (or both), so by asking for these, you can figure out "where you are".

That will usually be sufficient to tell you the situation, especially if you use class types judiciously (for example, you can make your navigation controllers different UINavigationController subclasses just for the sake of knowing where you are later).

If you want a complete conspectus of the view controller chain to where you are, you can trace your way up through the chain recursively, like this:

func trace(_ vc: UIViewController) {
print(vc)
if let parent = vc.parent {
print("parent:")
trace(parent)
return
}
if let presenter = vc.presentingViewController {
print("presenter:")
trace(presenter)
return
}
print("done")
}

That example prints rather than accumulating a list of view controllers along with the nature of the connection between each of them (which is what you really need), but by calling it from the "last" view controller in the chain, you can get a mental picture of what the chain must look like at the point where you want to say "go no deeper".

Here's a more complete example that shows how to accumulate a backward trace into an array:

class MyVC: UIViewController {
func makeTrace() {
super.viewDidAppear(animated)
let result = self.trace([(.start, self)])
print(result)
}
enum Link: String {
case parent
case presenter
case start
}
typealias Chain = [(Link, UIViewController)]
func trace(_ chain: Chain) -> Chain {
if let parent = chain.last!.1.parent {
return trace(chain + [(.parent, parent)])
}
if let presenter = chain.last!.1.presentingViewController {
return trace(chain + [(.presenter, presenter)])
}
return chain
}
}

So result will tell you enough to know "where you are".

How to get all the application's ViewControllers in iOS?

Hooking in on the answer of Umka.

Without knowing exactly what you want to do, this might be totally overkill, but it might help you.

You could subclass the UIViewController (e.g. the AddToArrayViewController), and in the custom init method add it to some Singleton you have defined somewhere. That way, whenever you initialize a ViewController (which is a subclass of your AddToArrayViewController) it will be added to an array in your Singleton.

BUT, you need to make sure you also remove the ViewControllers when they are removed/cleaned up, otherwise you will just have a list of dangling pointers in that array, which is not really safe.

Get ALL view inside ViewController

You need to recursively process all subviews.

func processSubviews(of view: UIView) {
// 1. code here do something with view
for subview in view.subviews {
// 2. code here do something with subview
processSubviews(of: subview)
// 3. code here do something with subview
}
// 4. code here do something with view
}

You need to put your code at either position 1, 2, 3, or 4 (or possibly two or more of those places) depending on the results you want.

Then you can call this as:

processSubviews(of: self.view)

How to iterate all the UIViewControllers on the app

Access them codewise like this:

NSArray * controllerArray = [[self navigationController] viewControllers];

for (UIViewController *controller in controllerArray){
//Code here.. e.g. print their titles to see the array setup;
NSLog(@"%@",controller.title);
}


Related Topics



Leave a reply



Submit