Ionic globally call function after Modal present - ionic-framework

I want to pop an alert box every-time after a modal is presented.
I know that I can do something like this modal.present().then(()=>alert("Modal presented"))
However, I have more than 30 pages, which have the modalController.
It is inefficient to add the promise calling to every pages.
Is there any way that I can globally customize the event after a modal box is presented, so that I need not to update all the pages?

You should create a function to show modal in your service. Every you want to show a modal, use that function. So you can handle success callback for every modal presentation.
yourProvider.ts
showModal(component, data) {
this.modalCtrl.create(component, data).present().then(() => {
console.log("show modal success");
//Add your alert here
alert("Modal presented");
}).catch(error => {
console.log("show modal error", error);
});
}
Just call the function above whenever showing a modal:
yourPage.ts
this.yourProvider.showModal("SomePage");
Note that: This is just a suggestion. I did not use this way in real project show can not point out any downside of it.

Check out Lifecycle events, in your case probably ionViewDidEnter().

Related

How to pass Activity type to a method in Xamarin.Android?

All my activities inherit from BaseActivity. In BaseActivity I have the following method:
protected void GoToPreviousActivity(Activity)
{
StartActivity(typeof(Activity));
Finish();
}
When I click the toolbar's back button of every activity I want to go back to the previous activity, like that:
toolbar.NavigationOnClick += delegate
{
this.GoToPreviousActivity(PreviousActivity);
};
How can I do that?
As #Mathias Kirkegaard commented, Android provides its own navigation back and it is very reliable (you can even manipulate the back stack (see the link at the bottom about what is it))
Having said that, if you want to use your method every time the user clicks on the back button you can override the OnBackPressed method, and provide your own implementation there.
In your case:
public override void OnBackPressed()
{
//base.OnBackPressed(); <-- this will use the default behaviour to navigate back, and if I understood correctly, you don't want to use it
GoToPreviousActivity(PreviousActivity);
}
Even though you can do that and it is valid, it is discouraged, you can read more about how Android manages the back navigation here:
https://developer.android.com/guide/components/activities/tasks-and-back-stack
and here is a good article on how to implement a custom back navigation: https://developer.android.com/guide/navigation/navigation-custom-back
should use OnBackPressed methode on click
base.OnBackPressed();

ionic Loading Controller Cancel and Return

This seems like a simple thing but I wasn't able to implement it and didn't find information on it.
I have an ionic Loading Controller that initiates when processing some data. This might take time so the user might decide to cancel. So I would like to simply add a cancel button in the loading element which dismisses the loading controller and returns to the previous page.
this.loading = this.loadingCtrl.create({
content: 'Loading ... (cancel button here?)'
});
this.loading.present();
As far s I know it is not supported. I did it with a modal, it works nearly the same and has the same syntax, but you don't have the transparent background.
See: https://www.techiediaries.com/ionic-modals/ for implementation.

How to Execute a Controller Method Every Time It Loads?

I'm struggling to find a way to execute a function in the main controller when it loads. When the main controller is loaded the first time, I can get that function executed inside onInit. But the issue is when user logs out and logs back in the main controller, the method onInit does not get executed again. Is there a way to execute a function every time when controller loads?
Below code will help you to achieve what your are looking for
onInit: function() {
this.getRouter().getRoute("routeName").attachPatternMatched(this._onObjectMatched, this);
}
_onObjectMatched: function() {
//this function executes every time you navigate to this page
}
Demokit link for detailed information
What do you mean by "when user logout and log back in the Main Controller"?? When your app loads the view the first time, it executes the onInit, onBeforeRendering, onAfterRendering and other lifecycle events. But if you don't destroy the controller instance you never 'log out' of it. It remains there as an object in your DOM and their functions can be called whenever is needed.
Now, if you are using the UI5 Router to navigate back and forth to other views, then I suggest you to set 'PatternMatched' events in your router. This events will be fired whenever the given pattern is match, no matter if it is the 1st or the n-th time.
Check out:
read optional url parameters
Step 32: Routing with Parameters

Ionic 2 app doesn't responde after modal dismiss

I have a modal in my Ionic 2 app. In this modal, clicking on a button, a confirmation prompt ("Are you sure?") pops up. By clicking yes, I do certain operations, then dismiss the modal by calling this method:
close() {
this._viewCtrl.dismiss();
}
and _viewCtrl is the property of my class, which i defined this way in the constructor:
constructor(
public _nav: NavController,
public _viewCtrl: ViewController,
public _profile: Profile,
params: NavParams) { ... }
My problem is that behind the modal I have a "3 Tabs Page", sort of. All works well, the 3 tabs are just fine, my modal is ok, it dismisses as it should, the problem is that when the modal is dismissed the 3 tabs break! They freeze on the second tab (which is the one that calls the modal)! Even if I click on tab 1 and 3, I see only the content for tab 2, and I can do nothing.
I'm pretty sure the operations before the dismiss shouldn't be a problem. I found that if I comment out the this.close(); part, and everything goes fine! The modal doesn't close, but I can close it by hand (with the same function, that's the strange thing!) then I return to the tabs page and the tabs are just fine.
What is happening to the app?
PS. No error is shown in the console!
If you check in the console you will see the modal overlay is still there. That's why when you click on any button on your app, you got no reaction cos you click on the transparent overlay.
I had similar issue now, where I had the dismissal of a Modal working before with Ionic 2 Beta 6, but on the latest Beta 10 it isn't working anymore.
How my logic worked was:
Do server call (while displaying progress with the Loading component
When call returns from server, then dismissing the Loading and then the Modal, but calling them in a synchronous fashion
Now this doesn't work with latest Beta. What I had to do was:
this.loading.onDismiss(() => {
this.viewController.dismiss();
});
this.loading.dismiss();
I got this workaround from here:
https://github.com/driftyco/ionic/issues/6325

iPhone: Handle action event from webview page

I am loading a html page using loadHTMLString.
[myWebview loadHTMLString:myHtmlStr baseURL:nil];
Its loading properly. In this html web page, i'm also adding buttons, drop down(with selection) etc. I want to catch the action events in my code now for these web page controls. For example, from the drop down if user chooses an option, i need to add another 'textarea' dynamically in the same web page. and, if user clicks on a button(button from the webview), i need to handle some events. I would like to know, how to do some tasks under the events triggered for controls in web view which is generated by my string.
Could someone please advise me.
to handle event on button in webview load a url in webview for ex:http://button1clicked and then in webView:shouldStartLoadWithRequest:navigationType: method check if the url is button1clicked then return no and also perform your action which you want to do on button clicked.
for example:
write this in webView:shouldStartLoadWithRequest:navigationType: method.
if([[[request URL] absoluteString] isEqualToString:#"http://button1clicked"])
{
//perform you action you want to do
return NO;
}
and on button click call javascript function window.location='http://button1clicked';
The documentation clearly states that UIWebView is not intended for Sub-Classing. However, you can still detect all the events.
But maybe this will help : Tutorial