NSNotification issue - iphone

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

Related

My alertview for the UIWebView doesn't work

Why doesn't this code work? All I have is this:
-(void)_webview:(UIWebView *)_webview didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"You have no internet connection!" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil, nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
exit(0);
}
It should work, right?
Because you mistyped the name of the UIWebViewDelegate method. You have
_webview:didFailLoadWithError:
whereas the real name of this method is
webView:didFailLoadWithError:
The delegate method is mistyped, as stated in the above response. Also, did you set the delegate of the UIWebView to the instance of the class that has those methods implemented?
for example, if it's a view controller, it could be in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
_webView.delegate = self;
}

UIAlertViewDelegate not acting as expected

I have a very simple process running where after each round of a simple game the scores are calculated, labels updated and all the normal, very simple stuff. I have a UIAlertView that informs the player of how s/he performed. I use a UIAlertViewDelegate to postpone all the updates, resetting of controls etc. till after the UIAlertView is dismissed. The methods are [startNewRound],[startOver] and [updateLabels]. It's fairly obvious what they all do. Anyway, when the user hits round ten, I've made another UIAlertView that informs the player that the game has ended and shows the overall score. Again, I hoped to use a delegate to postpone the resets till after the AlertView is dismissed. The only problem is, with the endGame AlertView, it seems to be using the first AlertView's delegate method causing the game to continue with a new round and not start from the beginning. I hope this makes sense. Anyway, here are snippets of my code.
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
[alertView show];
}
And then the delegate methods:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startNewRound];
[self updateLabels];
}
- (void)endGame:(UIAlertView *)endGame didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startOver];
}
So there it is. As I mentioned, the endGame AlertView appears to be using alertView's delegate, thus not activating the [self startOver] method. All the methods are working, it's just the AlertView is using the incorrect delegate method. Regards, Mike
Change your code like this,
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
endGame.tag = 111;
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
alertView.tag = 222;
[alertView show];
}
and delegate method as,
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 111)
{
[self startNewRound];
[self updateLabels];
}
else if(alertView.tag == 222)
{
[self startOver];
}
}
You cant have two delegate method for dismisswithbuttonindex, you need to handle this situation with tag.
Give both alert view a different tag and check it on delegate object. Thus you can differentiat the both alert view.

dismissing a UIAlertView programmatically

I need help on dismissing a UIAlertView programmatically. Currently I have this
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
then later on I call this
[alert1 dismissWithClickedButtonIndex:0 animated:NO];
but nothing happens.
You need to set two things.
1. include your .h file : <UIAlertViewDelegate>
2. please follow below implementation...
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
[self performSelector:#selector(dismiss:) withObject:alert1 afterDelay:1.0];
the dismiss method will be...
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
I hope this will help you.
I encountered this problem too.
In my case, for some reason calling:
[alert dismissWithClickedButtonIndex:0 animated:NO];
didn't work always (yes, even calling it on UI thread and yes, alert != nil), instead simply setting the animated flag to YES it worked:
[alert dismissWithClickedButtonIndex:0 animated:YES];
Maybe it's an Apple bug...
you should display it first:
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert1 show];
then in delegate method
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex==0){
// do something
}
}
The methods you called is correct.
I guess the alert1 is nil when your call the method dismissWithClickedButtonIndex:animated:
Try to check your variable alert1.
You can use the delegate method -alertView:didDismissWithButtonIndex: instead—it gets called once the alert view’s been removed from the screen, OR better approach is , use a background thread, e.g. with -performSelectorInBackground:withObject:, to handle whatever processing you need to do.

Executing a snippet of code in the AlertView OK button declaration

in my Alert View, there is two button, OK and Cancel. When the user click the OK button, the delegate method dismissWithClickedButtonIndex:animated get called, and if the index is 0, then i get called to a method to execute some code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Are you sure you want to exit"
delegate:self cancelButtonTitle: #"OK"
otherButtonTitles: #"Cancel",nil];
[alert show];
[alert release];//release the reference
Delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
[self aMethod];
}
}
-(void)aMethod{
//Some useful code
}
Now, what i want to instead of all this, is to execute the code of the aMethod method in the AlertView directly, without referring to A delegate method and a method which get called, something like that:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Are you sure you want to exit"
delegate:self cancelButtonTitle: #"OK" //Put here some useful code
otherButtonTitles: #"Cancel",nil];
Is it possible?
Unfortunately this is not possible at this time (iOS 5.1). The AlertView class does not support blocks.
I made a pair of UIAlertView and UIActionSheet subclasses that do exactly that. Grab them here:
https://github.com/rydermackay/RMActionSheet
Use them like this:
RMAlertView *alertView = [RMAlertView alertViewWithTitle:#"Alert!" message:nil];
[alertView addButtonWithTitle:#"OK"
action:^{
NSLog(#"OK");
}];
[alertView addCancelButtonWithTitle:#"Cancel"
action:nil];
[alertView show];
EDIT:
From your comments it sounds like you're not familiar with blocks. Read this now. Seriously.
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html
This is a good one too:
http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html

How to set actions for Push notification alert view in iOS

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.