I am using this code to implement a UITextField in my UIAlertView:
UIAlertView *receivedAlert = [[UIAlertView alloc] initWithTitle:message message:[NSString stringWithFormat:#"%#\n\n", [itemNameRows objectAtIndex:currentItem]] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
receivedField = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,25)];
receivedField.keyboardType = UIKeyboardTypeNumberPad;
receivedField.textAlignment = UITextAlignmentCenter;
receivedField.borderStyle = UITextBorderStyleRoundedRect;
receivedField.keyboardAppearance = UIKeyboardAppearanceAlert;
[receivedAlert addSubview:receivedField];
[receivedAlert show];
[receivedAlert release];
The problem arises when I have a message that is longer than 2 lines. The alert view resizes properly but the text field doesn't move down to accommodate the longer line. Any ideas?
I can settle with shortening the message string if I have to.
You shouldn't add subviews to UIAlertView. Apple's docs explicitly prohibit it ("The view hierarchy for this class is private and must not be modified."), and in addition to possibly getting you rejected, could also break in a future OS update (this happened once already around the iOS 3.1).
iOS 5 added alert view styles so that you can use text fields in alert views in a safe way. In addition to UIAlertViewStyleDefault, there are UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, and UIAlertViewStyleLoginAndPasswordInput.
Create an alert view and set the alertViewStyle property to the appropriate value. You can then get the text field(s) by using -textFieldAtIndex:. Secure and plain text styles have a single text field at index 0, default has no text fields, and login and password have two text fields at indexes 0 (login) and 1 (password).
See the UIAlertView Class Reference for more information.
Related
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"HELOOOOOOOO" message:#"write a number of text-lines/paragraph here"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
If you look close you will find that the message attribute is quite long. I need this alertview to first display the title, and then my long message, and then the text field and finally the 2 buttons.
But what hapence here, is that since the message is too long, the textfield overlaps with the buttons.
How can i solve this ?
If this is for iPhone, you really can't. The UIAlertView is not meant to handle input. Extended messages are shown with a scroll view but you really should not add a UITextField to it's view hierarchy (for one, it goes against their design standards and your app MAY get nixed!).
In this situation, it would probably be best to use a new UIViewController to handle showing your content.
Again, the only "actions" that UIAlertView is meant to provide is that of the multiple buttons.
Implement this piece of code in your implementation file.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
alertview.frame = CGRectMake(12, 95, 260, 25); //You can set a frame that suits to your needs
}
This delegate - (void)willPresentAlertView:(UIAlertView *)alertView will invoke before showing the alerview, so it is a better way to set frame to your alertview.
For more informations on aleert views:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate
Actually, it doesn't matter how long your message is. The alert view will always size so it fits.
One way you can fix this is by adding a bunch of \n at the end of your message string, which is equivalent to putting a line break.
EDIT: If your message is so long that it puts it in a UIScrollView, there's nothing you can really do unless you're fine with major hacking (aka, changing the bounds of the UIAlertView, moving each button down, etc.).
A second way works only on iOS 5 and newer. You can set the UIAlertView's alertStyle property to UIAlertViewStylePlainTextInput which will display an alert view with a text field.
Use newline or line break character \n for this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"AlertView"message:#"\nhello:.................... \nwrite here.................. \nwrite here....................." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Check this blog post for more help
First I would like to thank everyone who attempts to help me with my problem, I am new to iOS development, specifically objective-c, so I apologize if my question is extremely obvious.
I am making an app that loads some new data (parsed from a website but it is NOT a UIWebView) into the same current view every time the user changes a selection on the picker view (UIPickerView). The method that inserts this new data into the current view is called -(IBAction) getNewData: (id) sender.
getNewData is called every time the user makes a new selection with the picker. This is how it is called within the picker method and the picker as well as everything else works fine.
- (void)pickerView:(UIPickerView*)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *choosen;
choosen = [currentLottoNames objectAtIndex:row];
[self getNewData:choosen];
}
I would like to implement an activity indicator (spinner) for the loading time in-between the time the user makes/changes his selection on the scroller to the time the actual data shows up in the view after the user has made his selection on the scroller. How would I go about implementing this?
Thank you.
To show the user that you are accessing information via apples built in status bar you use
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
If you would like to display like a pop up message you need to declare in the header a
UIAlertView *load_message;
and then when you would like to show the load_message use
load_message = [[UIAlertView alloc] initWithTitle:#"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[load_message show];
UIActivityIndicatorView *active = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
active.center = CGPointMake(load_message.bounds.size.width / 2, load_message.bounds.size.height - 40);
[active startAnimating];
[load_message addSubview:active];
and that will show a pop up displayed to the user that you are loading something. This is for locking up the screen showing the user that you are getting some information.
I am trying to create the complex UIAlert View .But i am not getting right mark to get it work
Actaully i need the UIAlertView is like this
It has the png image on the leftside upper corner .And in same line it has the UIAlert titleBar.
Below that it has the message of two line
And it's below it has the TextFiled .and below the textfield it has
the once again same TextMessage
And last but not in list it has the Two button .
And ever user enter the text value in textfield and press the OK BUtton it get show on the label also .
I am trying coding like this But it not getting work what was the problem in it
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Set Tags"
message:#"Do you want to add tag.\nExample:-My Content"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message addTextFieldWithValue:#"" label:#"Enter tag Name"];
[message addButtonWithTitle:#"Cancel"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 30, 30)];
NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"tag.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[bkgImg release];
[path release];
[message addSubview:imageView];
if(FALSE)
{
[message addButtonWithTitle:#"Button 4"];
}
[message show];
[message release]
;
Help me out from this
Thank you.
Harish,
I am having a hard time understanding your english so I may not be hitting the mark with this answer. However, If I am understanding the question correctly then I believe I see only one major issue with your code.
From the documentation that I have read on UIAlertView I do not see the method "addTextFieldWithValue" as part of that class. There is however a reference to UIAlertViewStyle which is a property that can be set that will allow you to tell the class to display itself with a text field. You can find the class reference here
[message addTextFieldWithValue:#"" label:#"Enter tag Name"];
//This will not work.
//Instead use this.
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
Of course you will need to make sure you have the proper delegate methods added to retrieve the value once buttons are clicked.
I've taken your code and created an example project with only a few minor changes. Using my above suggestion the results I get are reflected in the screen shot I have taken below.
UIAlertView is not the component you should be looking to use in this case. It is not supposed to be customized. You should be displaying a modal view (which consists of all the items you listed) and display it using presentModelViewController:animated:.
I have an action sheet that is causing me grief on the iphone in Landscape orientation. Everything displays just fine, but in Landscape, the first real button has the same index as the cancel button and so the logic doesn't work.
I've tried creating the actionSheet using initWithTitle: delegate: cancelButtonTitle: destructiveButtonTitle: otherButtonTitles: but that was just the same, my current code is as follows;
UIActionSheet* actionMenu = [[UIActionSheet alloc] init];
actionMenu.delegate = self;
actionMenu.title = folderentry.Name;
actionMenu.cancelButtonIndex = 0;
[actionMenu addButtonWithTitle:NSLocalizedString(#"str.menu.cancel",nil)];
[self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu showInView:[self.navigationController view]];
[actionMenu release];
The addActiveButtons method basically configures which buttons to add which it does using code like this;
[menu addButtonWithTitle:NSLocalizedString(#"str.menu.sendbyemail",nil)];
There are perhaps 6 buttons at times so in landscape mode the actionSheet gets displayed like this;
My delegate responds like this;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"Cancel Button Index is : %d",actionSheet.cancelButtonIndex);
NSLog(#"Button clicked was for index : %d",buttonIndex);
NSString *command = [actionSheet buttonTitleAtIndex:buttonIndex];
DLog(#"COMMAND IS: %# for index: %d",command,buttonIndex);
if ([command isEqualToString:NSLocalizedString(#"str.menu.sendbyemail",nil)]) {
// Do stuff here
}
if ( ... similar blocks ... ) { }
}
In the example shown, I am finding that cancelButtonIndex is 0 as expected, but so is the button index for the first other button! This means if I click on the second (Save to Photos) button for example, my debug output looks like this;
Cancel Button Index is : 0
Button clicked was for index : 1
COMMAND IS: Send by Email for index: 1
I've tried various permutations and am now tearing my hair out wondering what I'm missing. I've had a good search around but the other problems people seem to be having are display issues, rather than functionality ones.
Can anyone see where I've gone wrong?
PS. I know this isn't the greatest UI experience, but I figure that most users will actually be in portrait most of the time or using the iPad version of the app so I'm prepared to accept the actionsheet default behaviour for landscape assuming I can get it to actually work!
OK, fixed it by counting how many buttons I was adding and then adding the cancel button as the last option, so my code looks like this;
int added = [self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu addButtonWithTitle:NSLocalizedString(#"str.menu.cancel",nil)];
actionMenu.cancelButtonIndex = added;
Hope that helps someone else struggling witht the same issue!
I ran into the same issue even though I already was including the Cancel Button as the last one in the action sheet and setting its index accordingly. My problems had to do with the 'Destructive' button. After some investigation, here is my take on the problem:
After N buttons have been added to the actionsheet, it switches it's layout to put the Destructive button at the top and the Cancel button at the button. In between is a scrollable view that includes all of the other buttons. Other sources indicate that this is a a table view.
For the iPhone, N is 7 for Portrait orientation and 5 for Landscape orientation. Those numbers are for all buttons including Cancel and Destructive.
It does not matter where in the action sheet you had originally put the Cancel and Destructive buttons within the action sheet. Once the limit has been reached, the Destructive button is moved to the top and the Cancel is moved to the bottom.
The problem is that the indices are not adjusted accordingly. So, if you did not initially add the Cancel as the last button and the Destructive as the first, the wrong index will be reported in actionSheet:clickedButtonAtIndex: as the initial report stated.
So, if you are going to have more than N buttons in your action sheet you MUST add the Destructive button to the actionSheet as the first button to the action sheet. You MUST add the Cancel button as the last button added to the action sheet. When initially constructing the sheet just leave both as nil, as described in another answer.
I had the same problem. To fix it, I just create an actionSheet with nil for all the buttons, and added buttons manually afterwards. Lastly, in the handler, ignore the firstOtherButtonIndex because it will be wrong (even if you set it ahead of time). Instead, assume that it is 1 because index 0 is the cancel button in this example. Here's the code:
NSArray *items = [NSArray arrayWithObjects:#"one", #"two", #"three", nil];
UIActionSheet* actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Title" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
[actionSheet addButtonWithTitle:#"Cancel"];
for (NSString *title in items) {
[actionSheet addButtonWithTitle:title];
}
[actionSheet addButtonWithTitle:#"Destroy"];
// set these if you like, but don't bother setting firstOtherButtonIndex.
actionSheet.cancelButtonIndex = 0;
actionSheet.destructiveButtonIndex = [items count]+1;
Also, don't forget to show this from a tab view if you're on an iPhone because the tab bar steals touch events and prevents the lower button from being hit.
My solution is to initialize like this specifying only the destructiveButtonTitle...
UIActionSheet * as =[[[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:#"Cancel"
otherButtonTitles:nil] autorelease];
[as addButtonWithTitle:#"Button 1"];
[as addButtonWithTitle:#"Button 2"];
That way you get the Cancel button at index 0 always and your own buttons begin at index 1 even when there is a scroll view.
I have an alert view for twitter posting.
Alert view has 2 button and a textfield
send and cancel
I want to disable send button, until user fills the message box(i.e textfield).
like,empty field kind of validation.
How can I disable send button?
I had a similar requirement and was able to do this without resorting to anything explicitly prohibited by Apple (ie, the use of private classes or API's). In the example below, I find and then disable the "Recover" button.
Note #1 -- The placement of "[alert Show]" is important. It (apparently) lays out the views, so must be done before attempting to look through the view hierarchy.
Note #2 -- the "contains:" method is one I defined that does an NSString case-insensitive substring search. Use rangeOfString perhaps in your code.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Application Warning"
message:#"What should I do with the file?"
delegate:self
cancelButtonTitle:#"Ignore"
otherButtonTitles:#"Remove", #"Recover", nil];
[alert show];
// try to find and disable "Recover" button
for(UIView *aView in alert.subviews)
{
if ([[[aView class] description] contains:#"Button"])
{
UIButton *aButton = (UIButton *)aView;
if ([aButton.titleLabel.text contains:#"Recover"])
{
aButton.enabled = NO;
}
}
}
This is not possible with the current SDK. You will have to create a custom view to take the user's input. The fact you are adding a textfield to the UIAlertView is itself unsupported and could break in any future SDK anyway.
I would suggest you create a custom view and if you still want it to look like a UIAlertView you can do this with appropriate images and custom buttons.