Pressing Cancel button doesn't retract - iphone

I am implementing an action sheet. When I press "ok" button, do these, press "cancel" go back. The "ok" button is working fine, but when I press the "cancel" button, nothing happens, it doesn't retract or do anything, just hang at the actionsheet view.
Below is my code:
To create button on the nav bar:
UIBarButtonItem *clearButton = [[[UIBarButtonItem alloc] initWithTitle:#"Clear History"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(ClearHistoryAction:)] autorelease];
self.navigationItem.leftBarButtonItem = clearButton;
When I click and launch action sheet:
- (IBAction)ClearHistoryAction:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Clear History?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"OK"
otherButtonTitles:nil];
// use the same style as the nav bar
actionSheet.actionSheetStyle = self.navigationController.navigationBar.barStyle;
[actionSheet showInView:self.view];
[actionSheet release];
}
if select ok do this:
- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if (!buttonIndex == [actionSheet cancelButtonIndex])
{
//do what i want here!
}
}
In the header file, UIActionSheetDelegate is included in #interface.

Late but, a possible Explanation could be :
Seems to be an issue with the tabbar. If you call UIActionSheet's
[sheet showInView:self.view]
from a view controller that is a child of a UITabViewController, then
the hit testing on the cancel button fails in that portion of the
UIActionSheet that lies above the tabbar's view.
If you instead pass in the UITabBarController's view, then the UIActionSheet acts as expected.
[sheet showInView:self.parentViewController.tabBarController.view];
A more detailed explanation here:
UIActionSheet cancel button strange behaviour

I don't know if it'll solve this problem but you'll want to do buttonIndex != [actionSheet cancelButtonIndex] (check not-equal) instead of !buttonIndex == [actionSheet cancelButtonIndex] (invert buttonIndex (!) and check if that's equal).

You need to use showFromBarButtonItem instead of showInView. The problem is that the cancel button is being covered by another view--maybe a toolbar or tabbar--so it's not getting the touch events you think it should get. There are also showFromTabBar and showFromToolbar that should sometimes be used (see the UIActionSheet class reference).

Related

Adding UITextField and UiButton on GooGle map

I'm new to iOS and working on Google Map SDK and able to show the map on the view but now
I want to add a UITextField and enter the location over there
On buttons click that location should be shown in the map
So please kindly help me out.
I would suggest you to add buttons on the UINavigationBar or by placing a UIToolBar on the top of the map view and then by clicking on it show a UIAlertView with textfield that takes in the input and after the alert's ok is pressed take the location entered and do your stuff. Don't add buttons on the map,it would bring down the usability factor.
If you have a navigation bar already, try this
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithTitle:#"location" style:UIBarButtonItemStyleBordered target:self action:#selector(showAlertWithTextField)];
self.navigationItem.rightBarButtonItem=locationButton;
-(void)showAlertWithTextField{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Location" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1)
NSLog(#"%#",[[alertView textFieldAtIndex:0]text]);
}

How the UIActionSheet works?

When popping a view I want to save some data, by asking confirmation. I'm asking confirmation using UIActionSheet. But irrespective of my response in action sheet, the view is changing in background, it creates some problem for me to use the response. I'm using navigation controller to switch views. How can I solve this
TIA
Better option is to use uialertview for asking confirmation.To do this follow this step:
Insure your header file contains the following:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Now when asked confirmation add this code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Are You Sure" delegate:self cancelButtonTitle:#"YES" otherButtonTitles:#"NO", nil];
[alert show];
[alert release];
Now after pressing one button below delegate will be called so add in .m file of app
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the YES/NO buttons
if (buttonIndex == 0)
{
NSLog(#"NO button pressed");
}
else
{
//Save data here
NSLog(#"YES button pressed");
}
}
#PooLas If I understood you correctly, You use uiactionsheet for user confirmation, while in background (actually under actionsheet) you change view controllers. Well, you can't do that, because delegate must be attached to controller which shows it up (if i am wrong, correct me). So when you click button, you can only first dismiss actionsheet and then change view controller, but not opposite – PooLaS

Problem in navigation Controller

I have a problem I want to control the backBarButtonItem when I click on it Action backAction "works
my problem is that SetAction does not work, I hope there is a solution for the controller backBarButtonItem
- (void)viewDidLoad {
[self.navigationItem.backBarButtonItem setTarget:self];
[self.navigationItem.backBarButtonItem setAction:#selector(backAction)];
[super viewDidLoad];
}
- (void)backAction {
if ((isSaveCarte==NO)&&(isNewCarte)) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Voulez vous Enregistrer la Carte" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView show];
[alertView release];
}
}
Check below, Could be useful for you
How to trap the back button event
http://forums.macrumors.com/showthread.php?t=637266
How to create backBarButtomItem with custom view for a UINavigationController
Assignig a action to backBarButtonItem
back bar button item is meant for navigating back...
why don't you use the leftBarButtonItem instead of backBarButtonItem..
the only difference would be it wont be a left pointed button.. but you are not navigating back anyways right?

Action sheet doesn't show Cancel button on iPad

On the iphone, this code shows the cancel button:
- (IBAction)buttonPressed
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Are you sure?"
delegate:self
cancelButtonTitle:#"No way!"
destructiveButtonTitle:#"Yes, I'm sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
But on the iPad, only the destructive button shows.
What's the problem?
This is part of the UI design and guidlines. Under 'Action Sheet' they say:
Do not include a Cancel button,
because people can tap outside the
popover to dismiss the action sheet
without selecting one of the other
alternatives.
It looks like the SDK hide the button for you on purpose. I'm not sure there is a solution, but maybe you could add your own button and set the cancelButtonIndex to match. Or you could switch to UIAlertView.
In iOS 5, this worked for me.
- (void)manualAddModel
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle: #"Select Equipment Type"
delegate: self
cancelButtonTitle: #"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: #"Add Boiler", #"Add Furnace", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery addButtonWithTitle:#"Cancel"];
[popupQuery showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Add Boiler");
}
else if (buttonIndex == 1)
{
NSLog(#"Add Furnace");
}
else if (buttonIndex == 2)
{
NSLog(#"Cancel Button Clicked");
}
}
Normally, tapping outside the actionsheet will serve the same purpose in iPad.
It looks like in iOS 4.2.1 you can manually add your own Cancel button like a normal button:
[actionSheet addButtonWithTitle:#"Cancel"];
And then set:
actionSheet.cancelButtonIndex = <your index>;
You won't get the red cancel button, but you will get either a blue or black one depending on your UIActionSheetStyle setting. In any event, it is pretty clearly distinguishable from the normal buttons and does correctly cancel.
Note that in my case I am showing an action sheet from within a popover controller, your results may vary in other scenarios.
I was able to solve this by setting the actionSheetStyle:
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIActionSheetStyleBlackTranslucent also works. I am displaying the action sheet from a modal view controller which I guess is not technically a "popovercontroller" like the guidelines say but not seeing a Cancel button on the action sheet doesn't look right when it appears on top of the modal view. All the user sees is one scary red button with no visible alternative.
Maybe I could change the modal view controller to a popovercontroller but then it wouldn't be modal which it needs to be.
--Update--
Well it was fun while it lasted but this no longer works in iOS 4.2.
I switched to using a UIAlertView instead of a UIActionSheet.
I no longer get a cool red button but it gets the job done.
I had the same issue when I tried to show ActionSheet in the View that was under another modal view, e.g. the view was invisible. Though the View wasn't nil looks like deep in framework it means so when it's not shown.
I solved the problem by setting a different UIModalPresentationStyle modalPresentationStyle property so the view became visible.
view.modalPresentationStyle = UIModalPresentationFormSheet;
According to iOS standard Cancel button is not displayed in UIActionSheet when displayed in iPad since UIActionSheet can be cancelled (Hide) by simply tapping any where outside ActionSheet region. In case of iPhone UIActionSheet will contain Cancel button.
Refer this link for further information UIActionSheet Cancel button in iPad

How do you create a multi button confirmation on the iPhone?

On the iPhone, in the Calendar App when you press the "Delete Event" button a confirmation slides in from the bottom. Does anyone know of any example code for this, or is it just a short view presented modally with a custom background?
If this is made using a custom view, do you know where I can get a background graphic the same as the one used in the Calendar App?
Thanks in advance!
NB: I am not talking about a UIAlertView dialog box, but the slide-in confirmation with multiple buttons.
UIActionSheet is what you are looking for.
Here is some code example to get you started with:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Save photo?" delegate:self cancelButtonTitle:#"No" destructiveButtonTitle:#"Yes" otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
This will slide in an action sheet from the bottom. It has 2 buttons. Yes and No.
When a user selects any button the actionSheet:didDismissWithButtonIndex: method gets called
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
//your code here
}
Your controller class will have to subscribe to the < UIActionSheetDelegate > protocol
Hope this helps!
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"title" delegate:self cancelButtonTitle:#"cancel" destructiveButtonTitle:#"destructive" otherButtonTitles:#"other", nil];
[actionSheet showInView:self.view];
[actionSheet release];