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];
Related
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
];
hi guys I have a method called manageui which display a waiting view for a while and when the times out it's display UIAlertView which display a message for try again
My problem is that i can't hide the UIAlertView before calling manageui called
here is my code :
-(void)mangeui
{
double Currenttime=0;
double ptime=Currenttime+5000;
NSLog(#"fire /n");
do
{
//add condition for found session
if (Currenttime<ptime)
{
NSLog(#"inside if");
[spinner setHidden:NO];
[alert setHidden:YES];
}
else
{
alert = [[UIAlertView alloc] initWithTitle:#"Oops:("
message:#"No device found \n Make sure bluetooth is activated and the devices are within range."
delegate:self
cancelButtonTitle:#"Tap to retry"
otherButtonTitles:nil];
[spinner setHidden:YES];
[alert show];
}
Currenttime+=1;
} while (Currenttime < ptime+1 &&[_matchmakingClient availableServerCount]==0);
}
the delegate for alertview is :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:1 animated:true];
[spinner setHidden:NO];
alertView.hidden=YES;
[self mangeui];
}
Try this:
[alertView dismissWithClickedButtonIndex:0 animated:YES];
Well I think you didn't understand the concept of delegates
here in docs it says alertView:clickedButtonAtIndex:
The receiver is automatically dismissed after this method is invoked.
yeah there is no need to declare separately to dismiss the alert view.The method is called whenever a button in alertview is pressed and the alertview disappears
I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
[self.navigationController popViewControllerAnimated:YES];
}
or
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iPoly"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
return;
}
both doesn't work
For this purpose you've to use UIAlertView's delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
First use this in your #interface <UIAlertViewDelegate>
Then set the delegate, self.yourAlertView.delegate=self;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//first button which should be the OK button
{
[self.navigationController popViewControllerAnimated:YES];
}
}
use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.
refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
or a simple tutorial on alert view
http://mobile.tutsplus.com/tutorials/iphone/uialertview/
You just need to implement UIAlerView Delegate Methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.
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;
}
}
Say I have a alert view like follows in obj c
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"title" message:#"szMsg" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:#"download"];
[alert show];
[alert release];
Now we have 2 buttons on the alert view (Ok & Download), how to write an event handler for the Download one?
First you will need to add the UIAlertViewDelegate to your header file like below:
Header file (.h)
#interface YourViewController : UIViewController<UIAlertViewDelegate>
Implementation File (.m)
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"title" message:#"szMsg" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"download"];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//Code for OK button
}
if (buttonIndex == 1)
{
//Code for download button
}
}
Now that most iOS devices have firmare versions with blocks support it’s an anachronism to use the clumsy callback API to handle button presses. Blocks are the way to go, see for example the Lambda Alert classes on GitHub:
CCAlertView *alert = [[CCAlertView alloc]
initWithTitle:#"Test Alert"
message:#"See if the thing works."];
[alert addButtonWithTitle:#"Foo" block:^{ NSLog(#"Foo"); }];
[alert addButtonWithTitle:#"Bar" block:^{ NSLog(#"Bar"); }];
[alert addButtonWithTitle:#"Cancel" block:NULL];
[alert show];
Declare your UIAlertViews as known.
UIAlertView *alertLogout=[[UIAlertView alloc]initWithTitle:#"Title" message:#"Stop Application?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alertLogout show];
[alertLogout release];
set delegate to self and implement this method.
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(actionSheet== alertLogout) {//alertLogout
if (buttonIndex == 0){
}else if(buttonIndex==1){
}
}else if (actionSheet==alertComment) {//alertComment
if (buttonIndex==0) {
}
}
}
Stack's and Guillermo Ortega's answer is probably what you would use with a couple of UIAlertView but not for ten. I use to use BlocksKit which is kind of the same as Lambda stuff which is what soul suggested. That is a good option too, although if you have too many nested blocks you will start seeing the demerits of it (Aside from the fact you will be relying in another library).
The usual way of handling several stuff would be to have a handler object. (
#interface MyAlertViewDelegate : NSObject <UIAlertViewDelegate> #end) make that object the delegate of the alert view and make sure the object is alive at least until the alert view is dismissed.
This will certainly work, but could be too much work...
What follows is what I came up with; IMO it is simpler and there is no need of any thirdParty library, or an ivar per UIAlertView. Just one extra object (#property (nonatomic, strong) NSArray *modalActions) to store the actions the current UIAlertView will cause to perform
Showing an UIAlertView and reacting accordingly
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:#"Blah blah"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:b1, b2, b3, nil];
// Add one selector/action per button at the proper index
self.modalActions = #[
[NSNull null], // Because indexes of UIAlertView buttons start at 1
NSStringFromSelector(#selector(actionForAlertViewButton1)),
NSStringFromSelector(#selector(actionForAlertViewButton2)),
NSStringFromSelector(#selector(actionForAlertViewButton3))];
[alertView show];
The delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.cancelButtonIndex != buttonIndex) {
[self performModalActionAtIndex:buttonIndex];
}
}
The part that actually performs the action:
- (void)performModalActionAtIndex:(NSInteger)index
{
if (-1 < index && index < self.modalActions.count &&
[self.modalActions[index] isKindOfClass:[NSString class]]) {
SEL action = NSSelectorFromString(self.modalActions[index]);
NSLog(#"action: %#", self.modalActions[index]);
if ([self respondsToSelector:action]) {
// There is a situation with performSelector: in ARC.
// http://stackoverflow.com/questions/7017281/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:action];
#pragma clang diagnostic pop
}
self.modalActions = nil;
}
Reusable for UIActionSheets too
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:cancelButton
destructiveButtonTitle:nil
otherButtonTitles:button1, button2, button3, nil];
// Similarly, add one action per button at the proper index
self.modalActions = #[
NSStringFromSelector(#selector(actionForActionSheetButton1)),
NSStringFromSelector(#selector(actionForActionSheetButton2)),
NSStringFromSelector(#selector(actionForActionSheetButton3))];
The delegate method:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.cancelButtonIndex != buttonIndex) {
[self performModalActionAtIndex:buttonIndex];
}
}
Why this works:
This works because of two reasons:
First, I never present two UIAlertView that have a delegate at the same time. (IMO you should't, it doesn't look good). Second, because in my case (as 90% of the cases) the target of the actions is always the same object (in this case: self). Even if you don't meet above conditions you can even use this approach with some modifications:
If you show two or more UIAlerViews or UIActionSheets at the same time (possible in the iPad) Use a dictionary with to store one array of actions associated with a certain UIAlertView/UIActionSheet.
If the target of the actions is not self, they you need to store pairs (target and the action) in the array. (Something to simulate UIButtons addTarget:action:...).
In either case, for storing the target and/or UIActionSheet/UIAlertView [NSValue valueWithNonretainedObject:] should become handy :)
First of all you declare UIAlertViewDelegate in .h file after put below code in .m file
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//put button action which you want.
}
}
Implement the UIAlertViewDelegate and make use of the delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
// Do something
}
else {
// Do something
}
}
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Data Saved" message:#"Choose more photos" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:Nil];
[alertView show];
[alertView release];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==0)
{
[self dismissModalViewControllerAnimated:YES];
}
}
in swift:
we can use this little block of code
let alert = UIAlertController(title: "Alert", message: "This is an alert message", preferredStyle: UIAlertControllerStyle.Alert)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in print("This is in alert block")
})
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)