"PopFrom" a view programmatically


I want to put in a ‘Home’ button which will pop any pushed views, so the user doesn’t have to keep pressing ‘back’. Is it possible to “PopFrom” a view programmatically? And to see how many are on the stack?



Hi. I’m still looking for an answer to this, if it’s possible. Can I do it through declares?


It sounds like what you’re really wanting to do is close views on the stack, in reverse order. I guess you could do that by building your own stack - an array of iOSView - and closing each one or just closing the first one that you Pushed which should close all the child views?

Thanks very much Jason. I’ll try that.

You could do that but I think you’ll see the animation of each view being closed in turn. That may or may not be a problem for you. I don’t believe there’s a cleaner way to do this currently.

you can go to the top of the stack:

[code]Sub goHome(animated as boolean=true)
Declare Function NSClassFromString lib “Foundation”(cls as CFStringRef) as Ptr
Declare function sharedApplication_ lib “UIKit” selector “sharedApplication”(cls_ptr as Ptr) as Ptr
dim shAppPtr as Ptr=sharedApplication_(NSClassFromString(“UIApplication”))
Declare function keyWindow_ lib “UIkit” selector “keyWindow”(app_ptr as Ptr) as Ptr
dim keyWinPtr as Ptr=keyWindow_(shAppPtr)
Declare Function rootWiewController_ lib “UIKit” selector “rootViewController”(winPtr as Ptr) as Ptr
dim rootWiewControllerPtr as Ptr=rootWiewController_(keyWinPtr)

Declare Sub popToRoot lib “UIKIT” selector “popToRootViewControllerAnimated:”(oPtr as Ptr,animated as Boolean)

popToRoot(rootWiewControllerPtr, animated)
End Sub

Nice work Antonio!!

Antonio, that’s cool, I didn’t realise you could do that, even from an ObjC/Swift app.

Very cool - and very helpful, too! Thanks for sharing this, Antonio.

Note that you can suppress the animation and return directly to the initial view, like this:

popToRoot(rootWiewControllerPtr, False)

Now you’re making it too easy for me Antonio :slight_smile:

Thanks very much - a great help