I'm implementing a very simple iOS application just to practice showing a pop up alert in, and I get an error when I press the alert button:
Thread 1:EXC_BAD_ACCESS(code=1,address=0x676f6f57)
This is the code:
- (IBAction)AlertButton {
alert = [[UIAlertView alloc]
initWithTitle:#"Alert" message:#"Alert"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Apple", "Google" ,nil];
[alert show];}
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"http://apple.com"]];
}
if(buttonIndex == 2){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"http://google.com"]];
}}
the problem is with the constructor of the UIAlertView, in the line:
otherButtonTitles:#"Apple", "Google" ,nil];
your forget the # before "Google". And finally change:
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
by
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
The real problem is you miss an # in front of "Google", so it is not an NSString and hence the crash.
use this
.h
no need for IBOutlet.just
UIAlertView *alert;
.m
alert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:#"Alert"
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Apple", #"Google", nil
];
Related
Yes, I've already Googled this question and I've used the exact code, but that's still not working.
Here is my current Objective-C code:
- (IBAction)btnTemp:(id)sender
{
if (_deepSwitch.on == TRUE)
{
[self TempCleaner];
_progress.progress += 1;
}
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[cleaned show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(#"clicked");
}
}
Why isn't this working? I've tried buttonIndex 0, and cancel button!
Set the alert view's delegate to self
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
Also, make sure that the class from which you are presenting the alert view declares the UIAlertViewDelegate protocol
//YourClass.h
#interface YourClass : SuperClass <UIAlertViewDelegate>
I'm kinda stuck with a stupid problem, in my app when a user press on a TableViewCell he can change some value in the server, before i am sending the information to server i want to show UIAlertView to the user to make sure he wants to do that.
so in didSelectRowAtIndexPath:
i wrote this
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Caution!" message:#"bla bla " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
then its calls the UIAlertView method clickedButtonAtIndex:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSString *url = #"some url";
NSDictionary *paramerter = #{#"parameter":parameter};
[self.jsonHandler startParseWithParameters:paramerter andUrlAdress:url withCompletion:^(int errValue) {
if (errValue == 1) {
NSLog(#"ERROR");
}else if (errValue == 0){
[self AlertViewWithTitle:#"success" andMessage:#"success"];
}
}];
}
}
the problem is that the if (buttonIndex == 0) called for every cell and change them all!
and i get UIAlertView message for every cell in the tableView.
how can i prevent it?
EDIT
Ok the problem caused because i use the same buttonIndex for both UIAlertViews
i solved it using this:
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"buttonString"]){
//Do what ever you need
}
hope someone find it helpful.
-(void)otherGames
{
UIAlertView *alertMsg = [[UIAlertView alloc]
initWithTitle:#"This gGame was Developed By:"
message:#"Burhan uddin Raizada"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles: #"#twitter" , nil];
[alertMsg show];
}
-(void)alertMsg:(UIAlertView *)alertMsg clickedButtonAtIndex:(NSInteger)buttonIn… {
if (buttonIndex == 1) {
NSString *containingURL = [[NSString alloc] initWithFormat:#"http://www.twitter.com/…
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: containingURL]];
}
}
the first alertmsg is working absolutely fine. but when i added a like to the new "#twitter" button, then it just doesn't work. otherwise everything is working fine. i am wondering why it doesn't, but it should..need help.
Assuming that
- (void)alertMsg:(UIAlertView *)alertMsg clickedButtonAtIndex:(NSInteger)buttonIn…
is
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
You must set your delegate to self and add the delegat protocol to your header :
#interface yourView : UIViewController <UIAlertViewDelegate>
Edit : According to #holex, use alertView:willDismissWithButtonIndex: instead of -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
UPDATE
this answer has been outdated since UIAlertView is deprecated in iOS8.
you can read more about UIAlertController on Apple's Dev Docs.
FIRST:
you haven't delegated your class, delegate:nil shows there is no delegated class for the UIAlertView. your should correct your method following this:
-(void)otherGames
{
UIAlertView *alertMsg = [[UIAlertView alloc]
initWithTitle:#"This gGame was Developed By:"
message:#"Burhan uddin Raizada"
// delegate:nil
delegate:self // don't forget to implement the UIAlertViewDelegate protocol in your class header
cancelButtonTitle:#"Dismiss"
otherButtonTitles: #"#twitter" , nil];
[alertMsg show];
}
SECOND:
the correct name of the callback method is: -alertView:didDismissWithButtonIndex:
//-(void)alertMsg:(UIAlertView *)alertMsg clickedButtonAtIndex:(NSInteger)buttonIn… { // WRONG, where have you got this silly idea...?
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *containingURL = [[NSString alloc] initWithFormat:#"http://www.twitter.com/…
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: containingURL]];
}
}
now, you know why your code fragment is wrong.
I want two alert views to show up only when the user opens my application for the first time -- the second to appear after the first is dismissed. I have it set up to only show the UIAlertViews when it has not been shown before and I do not need help with this. I need help figuring out how to display two alert views in a row when this is the case.
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex does not work for me.
Here is the code I have -- remember this is in didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:#"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:#"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
I'm gonna post a very simple solution using GCD & blocks (GCD part is just in case the alert view is created on another thread then the main thread, callback should be safe to perform on the main thread). Remember, I just coded this in like 5 mins, so you definitely should work on improving the code. One thing that's a bit ugly is the delegate parameter that is overridden in my subclass. The interface of the subclass could be changed a bit to make it more obvious of what happens ...
Anyway, here goes ...
First create a subclass of UIAlertView, make it look somewhat like the following ...
#interface FSAlertView () <UIAlertViewDelegate>
#property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
#end
#implementation FSAlertView
#synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
Now in the app we can create alert views like the following ...
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:#"Alert 1"
message:#"Some message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(#"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Alert 2"
message:#"Hi!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
If i understand your question correctly , then this may help:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:#selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:#selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This will show two alert views one after the other.
NOTE: I am not sure if you are dismissing the alerts with cancel button so i am dismissing them automatically after few seconds.
Try this:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView Delegate Method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Title 2" message:#"Message2" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(#"Code Here");
}
}
break;
}
}
I used Facebook SDK on my app. After i pressed "Add feed to your wall", the alert pops up with "YES" or "NO". If i choose "YES" the FBStreamDialoy just flash away.
First , click the button " Add feed to your wall" to call changeFeed: function:
-(IBAction) changeFeed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sporting Summer Festival Monte-Carlo" message:#"Are you attending this concert?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert show];
alert.tag = 1;
self.alertView =alert;
[alert release];
}
Then, press "YES" button. Call this function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"NO");
}
else
{
NSLog(#"YES");
[self showAddFeed];
}
}
And this is the showAddFeed function , which is defined in front of clickButtonAtIndex.
-(void)showAddFeed
{
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate= self;
dialog.userMessagePrompt = #"*****";
[dialog show];
}
Just cant work well. I don't know why? Thank you for your help.
------------Answer is--------------------------
[self performSelector:#selector(fbButtonClick) withObject:nil afterDelay:0.10];
Call it after a delay of 0.1 sec [self performSelector:#selector(fbButtonClick) withObject:nil afterDelay:0.10];
The problem is that you listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Which should be:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Because at the clickedButtonAtIndex function the view isn't closed and this can cause problems.
Edit:
To make it work in iOS 4 add your facebook login call to a method and call the following function in the didDismissWithButtonIndex:
[self performSelectorOnMainThread:#selector(facebookOpenAction) withObject:nil waitUntilDone:NO];