iPhone: Is it Possible to Hide and Unhide UIACtionSheet Buttons? - iphone

I am using FBConnect in my app. The log in action sheet buttons are title "Log in Facebook" and "LogOut Facebook" but I want to display "Log into Facebook" and "Publish to Facebook". Currently, it looks like this...
alt text http://freezpic.com/pics/6944f45f17ba4bbb8220637d5a00a1c6.jpg
...but I want it to look like this...
alt text http://www.freezpic.com/pics/93f28f4f9103f0842c849d7daa644f81.jpg
... possibly set in these methods:
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
//Show button log out
}
- (void)sessionDidLogout:(FBSession*)session {
//show button log in
}
Edit01- Alert sheet code from answer comment:
-(IBAction)mySheet:(id)sender {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Share On the Facebook" ,
#"Log in Facebook" ,
#"LogOut Facebook" ,nil];
[menu showInView:self.view];
[menu release];
}

Sure, just show a different UIActionSheet with just those two buttons depending on the state of the Facebook connection.
What about:
-(IBAction)mySheet:(id)sender
{
if (alreadyLoggedInToFacebook) {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Share On the Facebook" , #"Log in Facebook" ,
#"LogOut Facebook" ,nil];
} else {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"LogOut Facebook" ,nil];
}
[menu showInView:self.view];
[menu release];
}

Finally i implement that ! (alreadyLoggedInToFacebook) must be (season.isConnect) . every thing is good ! but still a problem . after login - logout and share show great but didn't work great ! it means if user tap Logout button , login window appears again ! why ? i think , its because of FBLoginButton , when delet this method my UIActionSheet doesn't Show
! here is my code :
-(IBAction)mySheet:(id)sender
{
if (session.isConnected) {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Share On the Facebook" , #"Log out Facebook" ,nil];
[menu showInView:self.view];
[menu release];
} else {
UIActionSheet *menu2 = [[UIActionSheet alloc] initWithTitle:#"FaceBook"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Log in Facebook" ,
nil];
[menu2 showInView:self.view];
[menu2 release];
}
}
- (void)actionSheet:(UIActionSheet *)menu2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [menu2 cancelButtonIndex])
{
FBLoginDialog* login = [[FBLoginDialog alloc] initWithSession:session];
[login show];
[login release];
}
}
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex2:(NSInteger)buttonIndex {
if (buttonIndex != [menu cancelButtonIndex])
{
[session logout];
}
}

Related

Number keypad jumping after UIAlertview

I have a login screen that shows an alert when the user gets the password wrong on a number of occasions. On clicking one of the buttons, another alert view is presented to confirm, if I press the cancel button on this second alert view, the number keypad bounces from the bottom of the screen back up to its original position. No code is being executed during this second alert response. Can someone help?
if (loginCount < 5) {
// Display alert to user
UIAlertView *loginError = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"You have entered an incorrect passcode. Please try again." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[loginError show];
} else {
// Display alert to user, including option to reset the app as they have 5 or more login failures
UIAlertView *loginError = [[UIAlertView alloc] initWithTitle: #"Login Failure" message: #"You have entered an incorrect passcode on 5 or more occasions. Please try again or reset the app." delegate: self cancelButtonTitle: #"Try Again" otherButtonTitles: #"Reset App", nil]
[loginError show];
}
// Clear password fields
[self clearPasswordFields];
[passcode1 becomeFirstResponder];
// Increment the login count
loginCount++;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// If the user has chosen to reset the app, alert with a confirmation first before resetting
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Reset App"]) {
// Create alert to give the user the choice to confirm reset
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm Reset" message:#"Are you sure you wish to Reset?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
} else if ([title isEqualToString:#"Yes"] && buttonIndex == 1) {
[Utilities resetApp];
[self dismissViewControllerAnimated:NO completion:nil];
}
}
This is how i will do it.
in the header file:
UIAlertView *loginError;
in the implementation file:
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (loginCount < 5) {
// Display alert to user
loginError = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"You have entered an incorrect passcode. Please try again." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[loginError show];
} else {
// Display alert to user, including option to reset the app as they have 5 or more login failures
loginError = [[UIAlertView alloc] initWithTitle: #"Login Failure" message: #"You have entered an incorrect passcode on 5 or more occasions. Please try again or reset the app." delegate: self cancelButtonTitle: #"Try Again" otherButtonTitles: #"Reset App", nil]
[loginError show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// If the user has chosen to reset the app, alert with a confirmation first before resetting
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Reset App"]) {
[self showSecondAlert];
} else if ([title isEqualToString:#"Yes"] && buttonIndex == 1) {
[Utilities resetApp];
[self dismissViewControllerAnimated:NO completion:nil];
}else{
[self clearPasswordFields];
[passcode1 becomeFirstResponder];
}
}
-(void)showSecondAlert
{
//make sure you dismiss the old alertview first
[loginError dismissWithClickedButtonIndex:0 animated:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm Reset" message:#"Are you sure you wish to Reset?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
Hope this helps...

How to dismiss alertview when click on the button in that alertview

I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}
Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}
You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}
Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.
For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.

How to assign the alert message textfield input value to the button?

How to assign the alert message textfield input value to the button?
In the below code, I am assigning value to textfield but requirement is assign value to button instead of textfeild through alert message help me to solve the problem in iPhone.
-(IBAction)alertmessage{
[self setingValue];
}
-(void)setingValue{
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year" message:#"alert message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:#"" label:#"entervalue"];
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
yearSelection.text= alertTextField.text;
}
}
if i enter keyboard value from alert textfield as input type that has to assign or appear on the button.
-(void)setingValue{
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year" message:#"alert message"
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertTextField=[UITextfiled alloc]initwithFrame:CGRectFrame:(5,25,200,35)]; //set the frame as you need.
alertTextField.Placeholder=#"Enter Value";
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert addSubview:alertTextFiedl];
[alertTextField release];
[myAlert show];
[myAlert release];
}

Detecting which UIAlertView was clicked

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...
Now when I clicked the ok button its calling again and again.. the alertview..
I need to call only once... what to do?
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
viewControllersList = [[NSMutableArray alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Alow this app to use your GPS location"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(#"NO");
}
else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
#thanks in advance.
set delegate:nil in second alertView
I mean
if (buttonIndex==0) { NSLog(#"NO"); } else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
Define each UIAlertView and in the delegate look for which Alert to respond to:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert1) {
if (buttonIndex==0) {
NSLog(#"NO");
} else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
} else {
/* the second alertview using the same buttonIndex */
}
}
You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"" message:#"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Erase", #"Cancel",nil];
alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up
[alert show];
then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0 && alertView.tag==123){
// Do what ever you wish
}
}
If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
}
http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4
Same for push notifications.
though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
#Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.

UIAlert View-for yes/no condition

my app needs alert msg and with yes button click another alert msg which decides the final action.
I have used - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
this method.
Please help me.
Do what #Adrian Pirvulescu said but before showing the alert do alert.tag = 1; and then when - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex is called do:
if (alertView.tag == 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"2nd Alert"
message:#"My message" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.tag = 2;
[alert show];
[alert release];
}
else if (alertView.tag == 2) {
[self CallSomeMethod];
}
else {
//do what ever you want here
}
Check this out
http://www.timeister.com/2010/06/objc-show-alert-iphone/
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlertView"
message:#"My message" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert release];