ionic 2 ViewController for popTo - ionic-framework

I want to use popTo ( http://ionicframework.com/docs/v2/api/components/nav/NavController/#popTo ) in my ionic 2 application. Which requires a ViewController ( http://ionicframework.com/docs/v2/2.0.0-beta.7/api/components/nav/ViewController/ )
I am not able to figure out how to create a page as a ViewController that is usable in popTo with the documents present over the internet.
Has someone used popTo ? any heads up !!

NavController.getByIndex(int index) returns a ViewController, so you can use: this.navCtrl.popTo(getByIndex(index)).
Don't forget to inject the NavControlelr properly
import { NavController } from 'ionic-angular';
class MyComponent {
constructor(public navCtrl: NavController) {
}
}
See here for Reference:
http://ionicframework.com/docs/v2/api/navigation/NavController/#getByIndex

this.navCtrl.popTo() also works if you provide a ViewController which is how I prefer to popTo(). It's dangerous to rely on the index because you may add a page to the flow which will change the index. This way you just give it the name of the page to pop back to.
let popToViewController = Utilities.getViewController(this.returnPageName, this.navCtrl);
this.navCtrl.popTo(popToViewController);
I had to create a utility method to obtain the ViewController from the NavController:
public static getViewController(pageName: string, navController: NavController): ViewController {
for (let viewController of navController.getViews()) {
if (viewController.name == pageName) {
return viewController
}
}
return null;
}

Related

SwiftUI - Display view on UIWindow

I'm trying to display a custom SwiftUI view similar to a Toast in Android.
My issue is that I would like to display this particular view above everything else, using the current UIWindow.
Currently, while working on static func displayToastAboveAll() located in my ToastView, this is how far i got
public struct ToastView: View {
static func displayToastAboveAll() {
let window = UIApplication.shared.windows.filter { $0.isKeyWindow }.first // window
let viewToShow = ToastView(my params) // my view to display
// This part I'm not sure of
let hostingController = UIHostingController(rootView: viewToShow)
window?.addSubview(hostingController.view)
}
public var body: some View {
// MyDesign
}
}
Any idea how should I use the window to put the ToastView at its proper place, and still being able to navigate within the app (and use the outlets) while having the view displayed ?
I managed to do what I wanted.
Basically, this code is working, but I had to remove some constraints from my SwiftUI view and add them with UIKit using the static func.
Also, I had to pass by a modifier (see below) and put ToastView init in private.
public struct ToastModifier: ViewModifier {
public func body(content: Content) -> some View {
content
}
}
extension View {
public func toast() -> some View {
ToastView.displayToastAboveAll()
return modifier(ToastModifier())
}
}
This is done to force the use of either modifier (SwiftUI, by doing .toast, just like you'd do .alert) or directly by calling the static func ToastView.displayToastAboveAll() (UIKit).
Indeed, I dont wont this Toast to be a part of the view, I want to trigger it like an alert.
Finally, special warning because passing ToastView into UIHostingViewController will mess with some of the animations.
I had to rewrite animations in UIKit in order to have a nice swipe & fade animation.

How to check if UIViewController is already being displayed?

I'm working on an app that displays a today extension with some information. When I tap on the today extension, it opens the app and navigates to a subview from the root to display the information. Normally the user would then click the back arrow to go back to the main view, but there is no way to tell if this is actually done. It is possible for the user to go back to the today extension and tap again. When this is done, the subview is opened once again with new information. If this is done a bunch of times, I end up with a bunch of instances of the subview and I have to click the back button on each of them to get back to the main view.
My question: Is it possible to check if the subview is already visible? I'd like to be able to just send updated information to it, instead of having to display an entirely new view.
I am currently handling this by keeping the instance of the UIViewController at the top of my root. If it is not nil, then I just pass the information to it and redraw. If it is nil, then I call performSegue and create a new one.
I just think that there must be a better way of handling this.
Edit: Thanks to the commenter below, I came up with this code that seems to do what I need.
if let quoteView = self.navigationController?.topViewController as? ShowQuoteVC {
quoteView.updateQuoteInformation(usingQuote: QuoteService.instance.getQuote(byQuoteNumber: quote))
}
else {
performSegue(withIdentifier: "showQuote", sender: quote)
}
This is different from the suggested post where the answer is:
if (self.navigationController.topViewController == self) {
//the view is currently displayed
}
In this case, it didn't work because I when I come in to the app from the Today Extension, it goes to the root view controller. I needed to check whether a subview is being displayed, and self.navigationController.topViewcontroller == self will never work because I am not checking to see if the top view controller is the root view controller. The suggestions in this post are more applicable to what I am trying to accomplish.
u can use this extension to check for currently displayed through the UIApplication UIViewController:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
and usage example:
if let topController = UIApplication.topViewController() {
if !topController.isKind(of: MainViewController.self) { //MainViewController- the controller u wish to equal its type
// do action...
}
}

How to decide what view to display based on global variable

I'm trying to implement an IOS swift app which starts off with a tab bar controller, and in one of the tab bar controllers is an item called "account".
When a user presses the account item, I want the app to decide (onclick event) whether the view that contains sign up/login is displayed or the profile view is displayed based on a global variable "loggedIn" (bool type).
(I've tried navigation controller but what I've understood from that is that it is a sequence of views which can't decide between views)
I want to know how can this be implemented, maybe some kind of "router" if you may that can switch between views...
If you didn't understand here's a picture of what I'm trying to implement
Basic Map of what I'm trying to explain
If you can suggest a more professional way of doing such design please don't hesitate to express your opinion.
I believe a good approach is updating the view controller when loggedIn status changes. If you don't already have, create a class inheriting from UITabBarController to manage your tabs. Here's the code:
class TabController: UITabBarController {}
In your storyboard, select your tab controller, go to the Identity Inspector and set TabController as the custom class. Now TabController will manage all the view controllers in your tab bar.
It's usually not a good approach to use global variables, so let's add loggedIn in the scope of TabController and listen for any of changes in it and update the corresponding view controller:
class TabController: UITabBarController {
var loggedIn = true {
didSet {
updateProfileTab()
}
}
}
Now, whenever you change loggedIn, that change will update the proper tab. Now let's write updateProfileTab():
class TabController: UITabBarController {
func updateProfileTab() {
let viewController: UIViewController
if loggedIn {
viewController = makeProfileViewController()
} else {
viewController = makeLoginViewController()
}
setViewController(viewController, at: 2)
}
func makeProfileViewController() -> ProfileViewController {
// create and return the profile view controller
}
func makeLoginViewController() -> LoginViewController {
// create and return the profile view controller
}
}
Naturally, you might want to write the body of both makeProfileViewController and makeLoginViewController methods. The last thing for TabController is to write setViewController(_:at:) method:
class TabController: UITabBarController {
...
func setViewController(_ viewController: UIViewController, at index: Int) {
let tabBarItem = viewControllers?[index].tabBarItem
viewController.tabBarItem = tabBarItem
viewControllers?[index] = viewController
}
...
}
Now, since TabController manages your tab bar, you can access it from within any of its children view controllers:
guard let tabController = tabBarController as? TabController else { return }
tabController.loggedIn = ...
Also, it's important to select the initial state. So, in the viewDidLoad from one of your tabbed view controllers, you should perform the above code. The first tab (the one that displays first) is probably the best place to do that. Hope this helps!
EDIT
To create your login and signup view controllers, the easiest way to go is by assigning ids in your storyboard. To do that, go to your storyboard, select the view controller, and in the Identity Inspector set a Storyboard ID, which you will use to instantiate the view controller:
func makeProfileViewController() -> ProfileViewController {
let controller = self.storyboard!.instantiateViewController(withIdentifier: "theStoryboardID")
return controller as! ProfileViewController
}
Note that I'm using force unwrap here (!). That's just for brevity. In a real case scenario you will want to use some if let or guard let statements to handle nil values.

How can I refresh an SKScene embed in a previous View Controller when dismissing View Controller with ARSKView?

In my SpriteKit game, I have two View Controllers. One is my MenuViewController, which has an SKView/SKScene in it - this handles the level selection, etc.
I then have my GameSceneViewController - this has an ARSKView/SKScene in it. When a user completes multiple levels and, for example, earns 3 stars for each one, and then uses the pause menu to go back to the MenuViewController, the stars that show for each level are (obviously) not refreshed.
This is because at the moment, when a user wants to go back to the MenuViewController, I simply call:
self.dismiss(animated: true, completion: nil)
I was wondering if there is a function I could call to "refresh" the MenuSceneController when a user dismisses the GameSceneViewController.
First of all add the following method to MenuSceneController
class MenuSceneController: UIViewController {
func reloadScene() {
// write here your logic to reload the scene
}
}
Now when you dismiss GameSceneViewController you can call that method
class GameSceneViewController: UIViewController {
func goBackToMenu() {
guard let menuSceneController = presentingViewController as? MenuSceneController else {
debugPrint("Error, this view controller was not presented by a MenuSceneController")
return
}
self.dismiss(animated: true) {
menuSceneController.reloadScene()
}
}
}

Monotouch open document - UIDocumentInterationController

I want to open a document on my iphone app written in monotouch
- i.e. launch a PDF file in the default PDF viewer.
I think I should be using UIDocumentInterationController?
Anyone have any Ideas on this..
I have put together the following on a viewcontroller ( with a toolbar)
but it doesnt work :-( It does nothing!!
string s = string.Format("{0}",strFilePath);
NSUrl ns = NSUrl.FromFilename (s);
UIDocumentInteractionController PreviewController =
UIDocumentInteractionController.FromUrl(ns);
PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass();
PreviewController.PresentOpenInMenu(btnOpen,true);
public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
{
public UIViewController FileViewController = new UIViewController();
public UIDocumentInteractionControllerDelegateClass ()
{
}
public override UIViewController ViewControllerForPreview (UIDocumentInteractionController controller)
{
return FileViewController;
}
public override UIView ViewForPreview (UIDocumentInteractionController controller)
{
return FileViewController.View;
}
}
First thing I would try, is ensuring when you present the options menu, that it is taking place on the main thread:
InvokeOnMainThread(delegate{
PreviewController.PresentOpenInMenu(btnOpen,true);
});
If that alone doesn't work, another thing I noticed is that you're creating a new view controller in the delegate class. It doesn't appear to be added to the stack anywhere in your code, so maybe thats why it's not showing. Code I've used is as follows:
PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);
...
...
public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
{
UIViewController viewC;
public UIDocumentInteractionControllerDelegateClass(UIViewController controller)
{
viewC = controller;
}
public override UIViewController ViewControllerForPreview (UIDocumentInteractionController controller)
{
return viewC;
}
public override UIView ViewForPreview (UIDocumentInteractionController controller)
{
return viewC.View;
}
public override RectangleF RectangleForPreview (UIDocumentInteractionController controller)
{
return viewC.View.Frame;
}
}
This will then use the current viewcontroller to present the preview in. The only other change I can think of using is rather than presenting from a UIBarButtonItem try:
PreviewController.PresentOpenInMenu(new RectangleF(320,320,0,500), this.View, true);
I hope this helps!