Navigation Controller Loop

Swift 4: Navigation Controller unwanted looping

I can see from your last screenshot that for your HomeAfterLogInViewController in the storyboard you have set the Custom Class class attribute to ViewController that means that you are linking that UI of HomeAfterLogInViewController with ViewController.swift and it seems you are in a loop.

Every storyboard UI ViewController must be linked to a swift class represent the specific ViewController logic.

That means in your last screenshot, where you see on the right Custom Class replace ViewController with HomeAfterLogInViewController (which should be the same name of a file you have as HomeAfterLogInViewController.swift)

enter image description here

Iterate view controllers of navigation controller swift

Optional chaining results in optional array of view controllers. Optional arrays do not conform to those protocols for be iterated with for..in loop. Try:

if let viewControllers = tabBarController?.viewControllers {
for viewController in viewControllers {
// some process
}
}

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);
}

How to put viewControllers into loop array?

In each iteration of the for loop, you are just setting a new array (with only one item) to viewControllers, which is why viewControllers will only have one item in the end as well.

You need to append the new VCs to the array, instead of using =:

let tabCount = selectedViewControllersProperties.count

// ..< is safer
var viewControllers = [] // you don't seem to have declared this beforehand.
for i in 0..<tabCount {
viewControllers.append(createController(title: "\(selectedViewControllersProperties[i])".localized, imageName: "\(selectedViewControllersProperties[i])", vc: selectedViewControllers[i]))
}

You can also do this with map:

viewControllers = (0..<tabCount).map {
createController(title: "\(selectedViewControllersProperties[$0])".localized, imageName: "\(selectedViewControllersProperties[$0])", vc: selectedViewControllers[$0])
}

how to create infinite looping in navigation controller like instagram application in iphone?

UINavigationController automatically supports this behavior you are describing. Memory efficient and potentially infinite.
Every time you want to go from one piece of your app to the next you just use code similar to this.

ViewController *vc = [[ViewController alloc] init];
//customize ViewController
[self.navigationController pushViewController:vc animated:YES];

So if you want to go from a photo to a photo detail view, you just push the photo detail view controller when the button is pressed. Even if your in a photo detail view controller already you can push another one from that class. Just make sure you create a separate instance. Judging by your error, I think that is your problem now.

I have also found it best to create a custom initializer for these kinds of view controllers. For a photo detail view use something like.

-(id)initWithPhoto:(Photo*)photo;

Loop of pushes in navigation view Controller

No it won't create any memory issues. iOS takes care it for you. When it present's another view, previous view gets deallocated automatically if you don't have strong reference cycle(always have weak reference to the parent in child viewcontroller). You can try adding initWithCoder and dealloc methods like below in each view controllers and see what happens.

- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
NSLog(@"init ViewController");
}
return self;
}

- (void)dealloc
{
NSLog(@"dealloc ViewController");
}

Also add viewDidAppear - ViewDidDisappear methods to see exactly what's happening. It's really interesting. Let me know if this doesn't make sense.

P.S see Beginning Storyboards in iOS 5 tutorials for details.

How to iterate through each Navigation Controller cointained in a Tab Bar Controller

for (UIViewController *viewController in self.tabBarController.viewControllers)
{
// Do your thing...
}


Related Topics



Leave a reply



Submit