iphone sdk calling dismiss alert function from another class? - iphone

how can i call dismiss alert function from another class?

Set the alert delegate to class where you want to receive button callbacks..

Related

Function after presenting Alert Swift

TLDR
Is there a way to be notified before a UIAlert is going to be presented. Is there a viewDidLoad/viewWillLoad type function that can be called in a ViewController either before or after an alert pops up?
My Problem
My View Controller is receiving an alert from a method in my app delegate. My View Controller calls a method in the app delegate which can then send a UIAlert if there was a problem. While this seems like bad design, I can't change it. I need some type of way of knowing that an alert showed up.
You can try
// do before
self.present(alert,animated:true) {
// do after
}
Sol1:
Add a completion block when you call the Appdelegate func and inside app delegate return that completion like code above in // do after
Sol2:
set a delegate between app delegate and the VC that calls it
Sol3:
use
NotificationCenter.default.addObserver(
self,
selector: #selector(listenForNotification),
name: Notification.Name(rawValue: "AlertShowed"),
object: nil)
inside the VC
and this in Appdelagete alert completion
NotificationCenter.default.post(name: Notification.Name("AlertShowed"), object:"")
The function present(_:animated:completion:) takes an Optional completion handler, completion. If you provide a completion handler it gets called once the modal is displayed. You display UIAlertControllers using present(_:animated:completion:), so just pass in a completion handler.
If you are opening the alert from within a method in appdelegate, then you should some how keep a reference on your view controller and call a specific method on it before showing the alert.
Or even, you can implement a pub sub design where you’d have your controller register for a custom notification name on the Notification Center and call/post that notification from the appdelegate just before showing the alert, don’t forget about removing the observer once the controller gets closed.
The choice is yours

Why would alert view delegate method not get called?

In my interface file I said I conform to the UIAlertViewProtocol and I implemented the alertView:clickedButtonAtIndex: method in my implementation file, and normally whenever the alertview button is pressed (the button that makes the alert go away)that method gets called. Well, it gets called most of the time, but for one of my alert views it doesn't get called after I press the cancel button on it, what would be a reason for this?
Oh Its because in one of my alert views for the delegate parameter i passed in nil instead of self, thats embarassing.

Is it possible to switch a view without a button?

I got only so far to be able to switch a view by pressing a button or with a timer. But I don't get it to switch a view by events like didReceiveLocalNotification. Is it impossible to do this?
Yes, that's possible , Because usually we specify the method with notification in which we wish to receive it.
Put the same code from your UIButton action: to notification method.
Yes, all you have to do is doing the switch inside the method didReceiveLocalNotification.
You will need to send a message to the view controller you want to perform the switch.

Is there a way to dismiss an alertview automatically after some time?

I'm looking for a way, to automatically dismiss an alert view after some time or after a task is done.
Is there a possibility? (or another way to show a message for some time?)
You can call the -dismissWithClickedButtonIndex:animated: method to dismiss the alert view.
To dismiss it automatically, create an NSInvocation and then use -performSelector:withObject:afterDelay: to -invoke it.
UIAlertView has a method called:
- ( void )dismissWithClickedButtonIndex: ( NSInteger )buttonIndex animated:( BOOL )animated
You can call it on your UIAlertView object to simulate a button press.
To dismiss it automatically after some time, you will need something like an NSTimer, to check if the alert view is still displayed, and in such a case, dismiss it.

UIAlertViewDelegate clickedButtonAtIndex: usefulness?

The UIAlertViewDelegate protocol defines two methods, alertView:clickedButtonAtIndex: and alertView:didDismissWithButtonIndex:, which seem to me to be identical in usefulness.
Why is there a clickedButtonAtIndex and a didDismissButtonWithIndex when they both do the same thing? I realize there is also a willDismissButtonWithIndex that happens before the alert view is dismissed, but is there any reason to use clickedButtonAtIndex instead of didDismissButtonWithIndex?
I found a more useful difference between the two:
When showing a UIAlertView, and the device goes to sleep, alertView:didDismissWithButtonAtIndex: gets called, even though the alert view is not actually dismissed. It is shown again once the device wakes up. alertView:clickedButtonAtIndex: is only called when the user clicks one of the buttons.
The alertView:clickedButtonAtIndex: is called when the user clicks a button on an alert view whereas the alertView:didDismissWithButtonIndex: is called after an alert view is dismissed from the screen. (See the UIAlertViewDelegate Protocol Reference.)
The difference is minimal but it allows you to do something before or after animation.
If the alert view disappears for any reason (including being covered by another UIAlertView, going to sleep, etc.), didDismissWithButtonAtIndex: is called. This can mean that the method can be called even without the user clicking on anything. This can lead to unexpected behaviour if you depend on this delegate callback to be called in response to the user actually clicking on a button. In this case clickedButtonAtIndex: is more useful.
I couldn't reproduce Ed's behaviour by locking my device with the alert view present on iOS 7.
However, the most important difference between alertView:clickedButtonAtIndex:, alertView:didDismissWithButtonIndex: and alertView:willDismissWithButtonIndex: is that the first method (clickedButtonAtIndex:) is only called when the user explicitly taps on a button on your alert view (hence 'clicked').
Is it possible that an alert view is dismissed without clicking on a button? Yes, you could programmatically hide an alert view using the UIAlertView method dismissWithClickedButtonIndex:animated:.
So, if you need some behavior to be always triggered upon the dismissal of the alert view—whether it was triggered by the user tapping on a button or triggered programmatically—then using the didDismissWithButtonIndex: and willDismissWithButtonIndex: makes more sense.