I have a problem with UIAlertView i do not understand. I cannot get it to work. I have changed the code during test so the UIAlertView should hit directly when pressing the button.
I have tested with breakpoint/debugger and what is happening when i press the button is that the first line that it stops at is "otherButtonTitles:nil];".
I did test the piece of code in an other part of the app and it worked.
I have no idea what i am doing wrong.
- (IBAction)startNewGame_Button:(id)sender {
UIAlertView *noPlayersAlert = [[UIAlertView alloc] initWithTitle:#"VARNING"
message:#"Ingen spelare är vald!\n \n Välj spelare och försök igen!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[noPlayersAlert show];
[noPlayersAlert release];
...
}
==============UPDATE=====================
When i copy this code to another button it works??
Here it is in one line, tested that with the same result:
UIAlertView *noPlayersAlert = [[UIAlertView alloc] initWithTitle:#"VARNING" message:#"Ingen spelare är vald!\n \n Välj spelare och försök igen!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
==============UPDATE #2==================
Problem solved, even if i do not understand the reason, as i removed an "exit(0)" i had far below the UIAlertView during the testing.
Thanks to all of you who answered :)
Why have you not set the delegate to self?
How do you know the line stops at otherButtonTitles:nil? If there is a crash log, post the results of it.
You must set delegate a value, because it needs to call dissWith, delegate method of the alertView. So please set:
delegate: self
Related
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
I need to me a UIAlertView viewer than the standard size.
This is what I'm trying so far:
UIAlertView *addNewDevice = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Add New Device", "Title for add new device") message:nil delegate:self cancelButtonTitle:NSLocalizedString(#"CANCEL", #"Cancel Button") otherButtonTitles:NSLocalizedString(#"OK", "OK"), nil];
addNewDevice.frame = CGRectMake(addNewDevice.frame.origin.x, addNewDevice.frame.origin.y, addNewDevice.frame.size.width, 500);
[addNewDevice show];
Yes I'm aware a width of 500 points is too big, but I merely put that value there to test and my UIAlertView is not modifying its size.
Admittedly I don't know much about dealing with objects programatically, but I have spent the afternoon reading about them with no luck regarding this. Any help will be appreciated.
Try putting the frame line after the show line.
[addNewDevice show];
addNewDevice.frame = CGRectMake(addNewDevice.frame.origin.x, addNewDevice.frame.origin.y, addNewDevice.frame.size.width, 500);
Hi i want to show my alertview message on large font so user can read proper when i run but it show me as small only how to resie font of message according to ipad i try this but it not working for me what to in my code help me on this
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"Flight Search" message:#"Please select both outbound and inbound flights" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
//UIAlertView* alert = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(100,0,340,200)];// message:#"Please select both outbound and inbound flights" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
//[alert setTitle:#"Flight Search"];
//[alert setCenter:CGPointMake(160.0f, 240.0f)];
//alert.frame = CGRectMake( 100, 0, 600, 500 );
//[alert setNeedsLayout];
[alert show];
alert.frame = CGRectMake( 0, 0, 340, 200 );
//alert.frame = CGRectMake( 100, 0, 600, 250 );
[alert release];
}
else {
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"Flight Search" message:#"Please select both outbound and inbound flights" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
From the UIAlertView docs:
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
You can't modify it.
Check this question/response to find out how to make a custom alert view:
default UIALertView cannot be modified as per Apple's Documentation , Instead make your own Custom AlertView
UIAlertView have UITextView inside with two or single button. So that's why it can expends over vertically but not Horizontally according to input. If you need to expand it ,increase message contents but you r not able to change font size.
You can do any thing with the default alert view. It will re size according to the text you have written. if you want to, then you need to create your own custom alert view. Check the link. put custom image and buttons and then you will be able to re size it.
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.
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:.