UIActionSheet long list behavior changed in 4.2? - iphone

I am seeing some changed behavior in iOS 4.2 with UIActionSheet. I can't find anything in Apple's developer docs or forums about it, so I'm not sure how to resolve it.
In my list app, I present the user with an actionsheet from which she can pick the list she wants to load on startup. Obviously that means there will be a variable number of items, and the control handles it fine. Until about 7 items, it shows all items as buttons. Once it crosses that threshold, it puts the items into a scroll view to choose from. Until 4.2, it included the Cancel button in that scrolling list. In 4.2, it now seems to be separating the Cancel control, leaving it as a button while putting the rest of the items into the scroll view. The problem is that it appears that it keeps the Cancel item in the list of button indices, so that when I inspect the buttonTitleAtIndex:buttonIndex in either clickedButtonAtIndex: or didDismissWithButtonIndex:, the first item returns "Cancel", then the other item titles are off by 1. Clicking the Cancel button also returns "Cancel".
Anyone else experienced this and have a suggestion for how to handle it? Again, this worked fine in 3.0, 3.1, 4.0, and 4.1.
Here's the relevant code I'm using:
- (IBAction)popupDefaultListActionSheet {
UIActionSheet *popup = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (List *l in allActiveLists) { // allActiveLists defined elsewhere
[popup addButtonWithTitle:[l label]];
}
popup.actionSheetStyle = UIActionSheetStyleBlackOpaque;
popup.tag = 23;
[popup becomeFirstResponder];
[popup showInView:[self.view.window.subviews objectAtIndex:0]];
[popup release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
DLOG(#"AppSettingsVC.actionSheet didDismissWithButtonIndex: %d", buttonIndex);
NSString *defaultListName = [actionSheet buttonTitleAtIndex:buttonIndex];
DLOG(#"chosen default list was: %#", defaultListName);
}

Try adding the cancel button dynamically at the end instead of setting it up initially:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"My Action Sheet"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (I32 i = 0; i < itemCount; i++) {
[actionSheet addButtonWithTitle:itemText];
}
[actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet setCancelButtonIndex:itemCount];
Seems to work correctly in iOS 4.2 for us at least.

Related

LongPress on iPhone app opens 3 Alerts? AlertView or Gesture Code Issue [duplicate]

I created an application. Through developing it, i used long press button to show an alert.
This is my code:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
// label1.text = #"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
Question: When I want to cancel the UIAlertView by using the cancelIbutton, I must press this button three times to cancel the UIAlertView. it means UIAlterview cannot be canceled just by one press.
Could you please help me?
The problem is that your are showing more than one alert view. Your long press handler will get called for different states.
From the docs for UILongPressGestureRecognizer:
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.
So you end up displaying an alert for the "Began" state, then the "Changed" state, and again for the "Ended" state. If you moved your finger around you would get a stream of "Changed" states and you would end up showing an alert for every one of them too.
You need to decide when you actually want the alert to appear. Do you want it to appear at the first "Changed" state or when the user lifts their finger and you get the "Ended" state.
Your code needs to be something like this:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
// label1.text = #"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
This will show the alert when the user lifts their finger. Change UIGestureRecognizerStateEnded to UIGestureRecognizerStateChanged if you want the alert to appear as soon as the long press is recognized. But keep in mind you may still get multiples since a long press can generate multiple "Changed" states. In this case you need to add an additional check to only show the alert if there isn't already one.
Actually, here's an easy way to support a single alert on the "Changed" state:
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
sender.enabled = NO; // Prevent any more state updates so you only get this one
sender.enabled = YES; // reenable the gesture recognizer for the next long press
// label1.text = #"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
Try this
- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender {
// label1.text = #"Select Iran to observe its historical data projections ";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Press the blue button (+) to select your region "
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
alert.tag =10;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (alertView.tag)
{
case 10:
if (buttonIndex==0)
{
}
break;
}
}
This is an old question, but I had a similar issue.
Any UILongPressGestureRecognizer will generate at least 4 changes of state:
changed - (i.e. changed to started)
began - (i.e. started)
changed - (i.e. changed to ended)
ended - (i.e. ended)
So to handle it well, you need to choose which of those conditions will activate an action.
The simplest, which sort of corresponds to 'touch up inside' for a UIButton is to detect the 'ended' state.
An example is below where 'longPress' is the instance of UILongPressGestureRecognizer
- (void)longPressedLastImage:(UILongPressGestureRecognizer*) longPress {
if (longPress.state == UIGestureRecognizerStateEnded) {
// do what you would in response to an equivalent button press
}
}
This lets you treat the continuous press more like the basic UIButton action.
'simple is better than complex' - T Peters - the Zen of Python

Custom UIalertView in iOS5

Do anywho know a custom UIAlertView class what is working in iOS5 ?
I'm looking for a class like TSAlertView, with that I will able to put 2 buttons stacked into alert.
( http://cocoacontrols.com/platforms/ios/controls/tsalertview )
Thanx for help.
UIAlertView in iOS 5 has UIAlertViewStyles
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
EDIT Sorry for misunderstanding your problem. The alert view shown in the linked page is extremely easy to reproduce. Here's what I came up with:
I implemented this with a category for convenience but you could easily just implement it elsewhere. Basically what you do is add a cancel button and then hide it. That way there are three buttons as far as the alert view is concerned and it does not place the two visible buttons side by side. The category implementation is as follows:
-(void)showWithCutCancelButton{
// Make sure alert view will look right
if (self.cancelButtonIndex == -1 || self.numberOfButtons < 3) return;
self.clipsToBounds = YES; // or else cancel button will still be visible
[self show];
// Shrink height to leave cancel button outside
self.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 64);
}
Then you show this by calling:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"Message here" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Option1", #"Option2", nil];
[alert showWithCutCancelButton];

Why doesn't the UIAlertView work?

I am testing this piece of code but when I run it, it does not trigger the UIAlertView. When the code hits If (ongoingGame = YES) and NSLog it jumps directly to the 'otherButtonTitles:nil' without executing the UIAlertView.
Can someone please explain to me why it does not trigger it?
-(IBAction)continueGame_button:(id)sender {
//=====CHECK IF THERE IS AN ON-GOING GAME, IF SO CONTINUE=====//
AccessCurrentGameData *isThereAnOngoingGameFunction = [AccessCurrentGameData new];
BOOL ongoingGame = [isThereAnOngoingGameFunction checkIfGameOngoing];
[isThereAnOngoingGameFunction release];
NSLog(#"+ + +continueGame_button+ + +");
NSLog(#"ongoingGame = %#\n", (ongoingGame ? #"YES" : #"NO"));
if (ongoingGame == YES) {
NSLog(#"++++++++++++++++++");
NSLog(#"++++++++++++++++++");
NSLog(#"++++++++++++++++++");
NSLog(#"++++++++++++++++++");
NSLog(#"++++++++++++++++++");
//
UIAlertView *continueGame = [[UIAlertView alloc] initWithTitle:#"Fortsätta spel"
message:#"Det finns ett aktivt spel, klicka Spela eller Tillbaka"
delegate:self
cancelButtonTitle:#"Tillbaka"
otherButtonTitles:nil];
[continueGame show];
[continueGame release];
}
exit(0);
}
You are assigning onGoingGame to YES, not comparing it to YES. Use == instead of =.
Your alert code is just fine I use that form (three lines - init, show, release) all of the time to do alerts.
I suggest that the exit(0) is the root of the problem. If you want to exit after the user closes the alert, you should assign a delegate which will close the app when the user taps on the close button. Use your code, but remove the exit(0). Then implement the UIAlertViewDelegate as follows:
-(IBAction)continueGame_button:(id)sender {
//=====CHECK IF THERE IS AN ON-GOING GAME, IF SO CONTINUE=====//
AccessCurrentGameData *isThereAnOngoingGameFunction = [AccessCurrentGameData new];
BOOL ongoingGame = [isThereAnOngoingGameFunction checkIfGameOngoing];
[isThereAnOngoingGameFunction release];
NSLog(#"+ + +continueGame_button+ + +");
NSLog(#"ongoingGame = %#\n", (ongoingGame ? #"YES" : #"NO"));
if (ongoingGame == YES) {
NSLog(#"+++++++++ ONGOING GAME +++++++++");
//
UIAlertView *continueGame = [[UIAlertView alloc] initWithTitle:#"Fortsätta spel"
message:#"Det finns ett aktivt spel, klicka Spela eller Tillbaka"
delegate:self
cancelButtonTitle:#"Tillbaka"
otherButtonTitles:nil];
[continueGame show];
[continueGame release];
}
}
- (void) alertViewCancel:(UIAlertView *)alertView{
//If you have other alerts, you may want to check the title of the alert to
//make sure that you only exit when THIS alert is dismissed
exit(0);
}
Dont' forget to add the <UIAlertViewDelegate> code to your header (.h) file.
You can also use - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, if you want multiple buttons, with one of them being a specific "Quit" button.
Please note that Apple discourages using exit() in apps that are released to the App Store, and using it might get your app rejected.
You can try this line instead.
[[[[UIAlertView alloc] initWithTitle:#"this is my message" message:nil delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil] autorelease] show];
Also, I believe Apple does not advice using exit() within your application. They always want the user to use the "Home" button to exit an app. The exit() call is a hard exit and this might be the reason you are not seeing the Alert.
You should not release it immediately. And you exit the app even before the alert view gets a chance to display itself. :)
Your code will continue to run even when the alert view is visible.
Fixage
Remove the exit call
Don't release the alertview. Release it in it's owner's dealloc method.
Make the alert view an instance variable and add a retain property to it.
Initialize the alertview in its getter if it's not yet available.
Set it's attributes in the IBAction and show it.
Add the appropriate delegate methods.
If I wasn't writing this answer on an iPod touch I'd post some example code. You can find lots of such code with Google.
Also, if your app isn't English-only you should always use localization provided by Foundation. Otherwise you can get English text with default error messages and other UI elements.

UIAlertView not displaying complete message

I have the following function to display error messages to user. But it does not seem to show the complete message. It will display upto certain characters followed by ....
How can I make it show the entire message?
(void) showAlert:(NSString*)title forMessage:(NSString*) body
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:body delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
There is a way.
When you present your alert, you can just implement this method:
- (void)willPresentAlertView:(UIAlertView *)alertView {
alertView.frame = CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y -50
,alertView.frame.size.width, 300);
}
Adjust the height to fit your need. It will look something like this:
If you need to move the button around, you can just add new lines (\n) to your message, and it will move the button down.
As mentioned by sudo rm -rf, UIAlertView has a limit.
You can try creating your own "alert" that doesn't clip by creating a view controller and showing it using presentModalViewController:animated:.

Which button was tapped in alert view

I create alert view like this:
UIAlertView* av = [UIAlertView new];
av.title = #"Dealer offer Insurence";
[av addButtonWithTitle:#"YES"];
[av addButtonWithTitle:#"NO"];
How can I find out which button was tapped by user?
In what way does the documentation fall short? You could even copy code from one of the many code samples.