How do I send the iOS user an instant local notification? - objective-c

I simply want a notification to pop up when the user presses a button. No servers or timers needed. All the tutorials I can find seem to involve one of these two.

Maybe something like this:
-(IBAction) buttonPressed: (UIButton *)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: [NSString stringWithFormat:#"You pressed button %#", sender.tag]
message: #"message"
delegate: self
cancelButtonTitle: #"Ok"
otherButtonTitles: nil]];
[alert show];
}
It would look something like this:
You can also customize the alert view a bit, so it could potentially have a textfield for text entry:
With UIAlertViews, you can also implement protocol methods like – alertView:clickedButtonAtIndex: and – alertView:willDismissWithButtonIndex:to perform different actions depending on which alert button they pressed.
Here's a good tutorial on UIAlertViews and implementing their protocol methods: http://mobile.tutsplus.com/tutorials/iphone/uialertview/.
If you don't want to use UIAlertViews and instead want a more customizable modal view, check out these two great libraries called UAModalPanel and MJPopupViewController. You can check out the links for images, demos, and more info on the two libraries, including links to their github pages where you can download them.
Hope this helps!

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelMessage];
[alertView show];

It is important to make sure the alert is shown on the main thread, where all the UI is handled. Otherwise you may get some weird errors and/or crashes. You can use GCD to dispatch to the main thread:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert Test"
message:#"This is an alert test."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",
nil];
dispatch_async(dispatch_get_main_queue(), ^{
[alert show];
});

Related

UIAlertView makes the program crash

I've got a crash:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.'
And I could not find a solution for 2 days.
And here is the code:
[alert dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *noTicketAlert = [[UIAlertView alloc] initWithTitle:#"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:#"Tamam" otherButtonTitles: nil];
[noTicketAlert show];
I triggered this error by attempting to display an alert from a background thread. Fixed like this:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:...
[alertView show];
});
I got this error when presenting a UIAlertView normally (no funny button override stuff). It turned out that I was presenting it twice in quick succession. The fix in my case was to remove the erroneous duplicate call.
If you do need to present two alert views at close to the same time, and you get this error, then a fix that works (and addresses the error message itself) is to run the code on the main thread:
[[NSOperationQueue mainQueue] addOperationWithBlock:^
{
// Your code that presents the alert view(s)
}];
Yes, I've found the solution and I share that with you guys.
I tried to override the dismissWithClickedButtonIndex function, and sent unique buttonIndexes
like 9999 for each of my alerts.
That is,
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[self viewWillDisappear:YES];
if(buttonIndex == 9999) {
noTicketAlert = [[UIAlertView alloc] initWithTitle:#"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:#"Tamam" otherButtonTitles: nil];
[noTicketAlert show];
}
}
and if I want to display the noticketAlert, I call this method like :
[alert dismissWithClickedButtonIndex:9999 animated:YES];
If you have a custom button make sure you implement the delegate method:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return YES;
}
if this selector is not found then the program with crash..
For those looking for the Swift 2 answer to this issue, I ran into a similar problem and solved it with #Dave Batton's solution
dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("loginSegue", sender: self)
})

UIActionSheet pass touches through?

I'm looking to use a UIActionSheet kind of like a contextual message box (with no action buttons at all and just a label in the bubble with an arrow pointing at something). Since there are no actions the user can take, I would like it to not require a tap to dismiss, but I can't see any way (such as a passthroughViews property) to allow this.
It's probably not designed for this, but it does happen to be handy for it.
This is some example code of how to show an UIAlertView and dismiss it automatically.
Show it:
- (void)show
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"title"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
alert.tag = tag;
[alert show];
}
Dismiss it:
- (void)dismiss
{
for (UIWindow* w in [UIApplication sharedApplication].windows)
for (NSObject* o in w.subviews)
if ([o isKindOfClass:[UIAlertView class]]) {
UIAlertView *alert = (UIAlertView*) o;
if (alert.tag == tag)
[alert dismissWithClickedButtonIndex:[(UIAlertView*)o cancelButtonIndex] animated:YES];
}
}
Yo can call the dismiss method after a couple of seconds:
[self performSelector:#selector(dismiss) withObject:nil afterDelay:1.0 inModes:nil];

UIAlertView delegate methods don't response on iOS 5.1

I created an instance of UIAlertView with two buttons and in the interface file of my class(.h) I set the delegate too but still cant get any response when clicking the buttons. Here is my code:
//myClass.h
#interface MainMenu : UIViewController<UIAlertViewDelegate>
-(IBAction)secondAct:(id)sender;
And the implementation
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert autorelease];
}
and the delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"UIAlertView delegate works");//this line too isnt displayed
NSString *title=[ alertView buttonTitleAtIndex:buttonIndex ];
if ([title isEqualToString:#"OK"]) {
NSLog(#"OK Pressed");
}//i want to create something like this
}
I did all of the code above but still can't take any action. Whichever of the buttons i click, it dissmisses the alert. What is wrong with this code can any one help?
ANSWER
Dear PeterG. commented to change delegete:nil with delegate:self and it works now.
Delegate should probably be set to self. If it generates an error post it here.
Also I do not think you should autorelease the alert ivar. Try to skip that line and see what happens?
Just give the delegate "self".
-(IBAction)secondAct:(id)sender
alert = [[UIAlertView alloc] initWithTitle:#"Dear User"
message:#"Your Request Will be Sent To Security"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
}

UIAlertView with Button linking alternate ViewController

I needed a button that causes a UIAlertView with actions to pop up.
Once the Alert pops up it needs to have 1 button to cancel and stay on the same page and 1 button that links you to another ViewController.
This is what I pieced together from some forums but I have no idea what I'm doing and it gives me about 9 error messages. Please Help!
-(IBAction)Alert:(id)sender {
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Warning! By entering the Tutorial, all data will be lost. Are you sure you want to continue?"
delegate:self
cancelButtonTitle:#"Return to Data Collection"
otherButtonTitles:#"Continue", nil];
[Alert Show];
[Alert Release];
}
- (void)Alert:(UIAlertView *)Alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(Alert.tag==0) {
if(buttonIndex == 1)//OK button pressed
{
Tutorial *Info = [[Tutorial alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Info animated:YES];
}
The first box of code works so that when I press a button on my home screen an alert with 2 buttons pops up.
However, I can't get the second button to link me to the next ViewController.
Objective-C is case-sensitive.
[Alert show];
[Alert release];
and
- (void)alertView:(UIAlertView *)Alert clickedButtonAtIndex:(NSInteger)buttonIndex
(How do you think, it can work, if you rename the methods???)
remove if(Alert.tag==0) {
Why are you not passing a name for a nib-file here: Tutorial *Info = [[Tutorial alloc] initWithNibName:nil bundle:nil];
Please stick to coding conventions. objects are named in camelCase.
Conclusion
get you a good book or videos to learn from the beginning. Some resources to do so.

UIAlertView without any buttons

I wanted to know whether the following code is okay or not. I am trying to dismiss the alertView automatically after 2 seconds (and without any buttons in the alertView) from the "timedAlert" method.
//this is in another method
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert release];
[self timedAlert];
}
-(void)timedAlert
{
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:2];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
}
If the cancelButton of the alertView is set to "nil", how will the "[alertView dismissWithClickedButtonIndex:0 animated:YES];" thing work??? I tried making the cancelButton "nil" and it worked, but cant figure out how....
P.S: I call the timedAlert method from another
Any help is appreciated! Thank you!
First let me say it would be better if you handle this with a custom view, but with that said the problem looks to be with
[alert release];
You are releasing the object before you are done with it (I am surprise it does not crash).
Do something like this
// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:#selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}
-(void)dismissAlert:(UIAlertView *) alertView
{
[alertView dismissWithClickedButtonIndex:nil animated:YES];
[alertView release];
}
Your code should work, and you should have no problems. I have done this in one of my previous apps. The button is not displayed because the title is nil but I think the instance of the button still exists. Put a breakpoint before closing your alert and take a look at the alert variable, and check to see if there is a buttons array or something, that should tell you how that works.