How to set actions for Push notification alert view in iOS - iphone

In my application I have implemented the Push notification feature and I am getting the notifications. I used the following code in the appDelegate file.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSMutableArray *array = [userInfo objectForKey:key];
NSString *message = [NSString stringWithFormat:#"%#",[array valueForKey:#"alert"]];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"iPhoneApp" message:message delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
[alert release];
}
}
I want to perform actions on the OK button click event of the Push notification alert (when the app is running). I have three view controllers in this app. So in which class should I add the code
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex?

In the same class you set as the delegate of that particular UIAlertView. In your current case, the AppDelegate is the receiver of the -clickedButtonAtIndex:.
If you would like to receive the click events in one of the 3 controllers you have. You must set that particular controller as the delegate to your UIAlertView:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"iPhoneApp" message:message delegate:myViewController cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
[alert release];
As you see I assigned myViewController as the delegate. myViewController should conform to the UIAlertViewDelegate protocol and implement the -clickedButtonAtIndex: method. Now once you select one of the button's you will get the call in myViewController.

Related

iPhone alert view exclude a view controller

i have a project with many vie controller, in one of these i create and show a view alert. it's possible show alert in every view exclude one?
I need this because if you are in the alarm view controller you don't need to see the alert when alarm ring
i try this but not works!
// ALERT NOTIFICATION
if (!self.timerViewController) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Piccole Ricette" message:#"READY" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}
In iOS you can test whether a ViewController 's view is visible by testing the view's window property. If the view is not visible the window property will be nil. So perhaps you can do something like this:
if (!self.timerViewController.view.window) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Piccole Ricette" message:#"READY" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}

IPhone: How to call AlertView from a Static Function

I have a class as follows
#import "UtilAlert.h"
#implementation UtilAlert
+(void) showAlert:(NSString *)message andTitle:(NSString *)title andDelegate:(UIViewController *) delegate
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
#end
The problem is that when call the function with necessary parameters...
[UtilAlert showAlert:#"hello" andTitle:#"hello" andDelegate:self] ;
i get a error: Thread 1: stopped at break point 3;
for the function call from an UIController class
This is not an error. You have a breakpoint in your code, which is the little blue arrow in the left margin of your code. Click the blue arrow again to make it really light blue to turn off the breakpoint.
Also, you should release the alert after showing it before exiting the function or you will leak memory.
Just a wild guess, but your alert object may be released when the function exits if your are working on ARC. In that case, this might work :
+(void) showAlert:(NSString *)message andTitle:(NSString *)title andDelegate:(UIViewController *) delegate {
static UIAlertView *alert;
if(!alert)
alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}

NSNotification issue

i want to show the alert box in my whole applicaton when we got the response from my Signin
how it is possible?
can we use NSNotification?
You could put a public method in your appdelegate and let it show your alertview.
You can access the app delegate like this:
[UIApplication sharedApplication].delegate
You'll need to cast it to you app delegate class to prevent a warning, then you can send the message:
[(MyAppDelegate *)[UIApplication sharedApplication].delegate showMyAlertView];
- (void)afterSignIn
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
It's simple to create and receive notifications:
1) Add an observer (for example, it's YourViewController) to your notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someEventHappend)
name:#"SomeEvent" object:nil];
You should add this code in viewDidLoad method.
2) Implement someEventHappend method in YourViewController
3) Post notification when you've git response from Signin:
[[NSNotificationCenter defaultCenter] postNotificationName:#"SomeEvent" object:nil];
After that NSNotificationCenter will call someEventHappend method on YourViewController
#anil take one and set this in Global.h "BOOL Signin" value .When When it's true
Then show
Your alter view like this
-(void)afterSignIn
{
if(Signin == YES)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Meassage" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
it is not compulsory to use NSNotificationCenter

UIAlertView show when app loads, open

How show a alertview just when the app open, load...
Like everytime that the user open the app, he see the alert ....
write the alert statment in the following appDelegate method,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alert_View = [[[UIAlertView alloc] initWithTitle:#"Your Title" message:#"Your Message" delegate:self cancelButtonTitle:nil otherButtonTitles:#"YES",#"NO",nil] autorelease];
[alert_View show];
}
Show the alertview in the application delegate class, in applicationDidFinishLaunching: and in applicationWillEnterForeground:
#Rafeel put this UIAlertView in your didFinishLaunchingWithOptions method of your AppDelegate and you will always get the UIAlertView when your app launches
UIAlertView *confirmAlertView=[[UIAlertView alloc]initWithTitle:#"Saved" message:#"Want to save!!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel",nil];
[confirmAlertView show];
[confirmAlertView release];
Hope it Will Help u!

How to create a method to return UIAlertViews?

I have a question about showing an alert on the screen. The thing is: I have an app with 20 to 30 different screens (nibs) And in each nib I do some checking to see if the user has inserted text in a textedit. And some alert messages are identical to others. Like In 3 nibs there is a text field for the user to enter his age, and the an alert that shows up if he left it blank.
What i want to do is to create a method to show these alerts so I don`t need to have the same alert on different nibs. instead of calling an alert view in each nib, I would call the method and pass what kind of alertview to pop up.
What would be the best way to implement this method?
TIA.
You can just alloc init a new UIAlertView as usual but you have to remember to pass the delegate in.
Here is my method:
- (UIAlertView *)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
return [[[UIAlertView alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancel otherButtonTitles:nil] autorelease];
}
All right, I managed to do it. Thanks to all that helped. This is my final solution
I created a common.m classs for some methods that I use in various nibs.
Common.h
#interface MetodosGerais : NSObject <UIAlertViewDelegate>{...}
- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel;
Common.m
- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
if (title == #"Enter your Height"){
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Atention!", #"Atenção!")
message:#"You Should enter Your Height."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
else if (title == #"Enter your Age"){
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Atention!", #"Atenção!")
message:#"You Should enter your Age."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
...
and in my classes that I want to use it I did
Common *myAlert = (Common *)[[UIApplication sharedApplication] delegate];
if ([idade.text length] == 0) {
[myAlert getUIAlertViewWithDelegate:self title:#"Enter Your Age" cancelTitle:#"OK"];
}...