Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
I was playing around with class-dump last night and I decided to dump the UIKit framework to see what goodies were in there that Apple wasn't telling us about. As it turns out (and it probably comes as no surprise) UIAlertViews have methods for adding text fields to their views without going through all the fuss that I made using transforms and such. I don't remember the methods right off the top of my head, but they're there and they work rather well. I don't know if Apple checks for this kind of stuff when checking apps for the AppStore, but I don't see why it would be that big of a deal since we aren't linking against any of the private frameworks.
As a side note, the UIGlassButtons are still referenced in the framework and with the proper header they can still be used in the simulator. However, the linker gives an error when trying to compile for the iPhoneOS saying that the object isn't found. It would be cool if we could get those back.
Anyway, if anyone wants the methods for adding text fields to their alert views, I can post them when I get home.
Alright, here's the goods. The first thing that you'll want to do is to alloc your UIAlertView and then initWithTitle:message:delegate:cancelButtonTitle: otherButtonTitles: just like normal. Then with all that good stuff done, you use the method addTextFieldWithValue:label:. The value is for initializing some text into the text field. The label is for setting a placeholder. Here's some example code.
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out. Then, to access the value that was entered into the text field when the ok button is clicked, you just do something like this. In this particular example, myString is an iVar that's already been alloc'ed and init'ed. Thus far, I've not figured out a way to make the text field return if the user presses the return button on the keyboard. I've tried all kinds of things and nothing that I've tried works.
I was able to get the return key to work as follows:
[LIST=1] [*] Add the UITextFieldDelegate protocol to your controller. [*] Set the delegate of the UIAlertView's text field to self. [*] Add the following function: [/LIST]
Alright, here's the goods. The first thing that you'll want to do is to alloc your UIAlertView and then initWithTitle:message:delegate:cancelButtonTitle: otherButtonTitles: just like normal. Then with all that good stuff done, you use the method addTextFieldWithValue:label:. The value is for initializing some text into the text field. The label is for setting a placeholder. Here's some example code.
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out. Then, to access the value that was entered into the text field when the ok button is clicked, you just do something like this. In this particular example, myString is an iVar that's already been alloc'ed and init'ed. Thus far, I've not figured out a way to make the text field return if the user presses the return button on the keyboard. I've tried all kinds of things and nothing that I've tried works.
I've tried to catch the returning value, but the delegate method doesnt get called.
I was able to get the return key to work as follows: Add the UITextFieldDelegate protocol to your controller. Set the delegate of the UIAlertView's text field to self. Add the following function:
1. UIViewController 2. [[myAlert textField] delegate:self]; ??? 3. I added the function
I am sure that Im not setting the delegate correctly. In the debugger, I dont see a delegate item for the textField.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if ([alertView title] == @\"New Alert\") { //Only for one specific alert
NSString *myString = [[alertView textField]text]; //Get the string NSLog(@\"User Pressed Button %d and String is: %@\", buttonIndex + 1, myString); //Put it on the debugger
if ([[[alertView textField]text]length] <= 0 || buttonIndex ==0) return; //If cancel or 0 length string the string doesn't matter
if (buttonIndex == 1) { //Setup an object or do sth. else here } } }
If you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out.
Im still trying to work this one out. Im telling my text view to become the first responder, but I still get two keyboards. They come up at almost the same time, but they delay and transparency give it away.
Yeah, I’ve definitely reviewed all of the code. I can access the text field just fine, but setting it as the first responder just doesn’t seem to work out. The double keyboard animation is almost seamless on the device, so I might just stick with it.
This is really strange. When I start it with Leaks, then it works perfectly; no setting of the first responder necessary. Attached is a sample project of the code; note that the keyboard is twice as dark as what it’s supposed to be.
Note that the sample project doesn’t demonstrate the "Leaks fixes it" randomness.
Well, I certainly don’t know what to say. It’s now working perfectly fine on the device (without becoming first responder). I will certainly try and track down what’s going on if it starts to not work again.
I want my application to be available in landscape and portrait, but when I'm in landscape and click the button for the UIAlertView, the alertview and keyboard and both portrait. Does anybody know how to make them landscape?
Oh, and when I press a button on the keyboard while STILL in landscape, the pop-up that shows what key you are pressing comes up where the key would be if it was landscape :S.
I want my application to be available in landscape and portrait, but when I'm in landscape and click the button for the UIAlertView, the alertview and keyboard and both portrait. Does anybody know how to make them landscape?
Oh, and when I press a button on the keyboard while STILL in landscape, the pop-up that shows what key you are pressing comes up where the key would be if it was landscape :S.
Your question has nothing to do with this thread. Have some respect and don't hijack threads...create your own if you want a response.
Your question has nothing to do with this thread. Have some respect and don't hijack threads...create your own if you want a response.
Yes, my question actually does. This thread is about adding text fields to alerts. I have an alert with a text field, and I was wondering if anyone knew how to make it work in landscape.
Yes, my question actually does. This thread is about adding text fields to alerts. I have an alert with a text field, and I was wondering if anyone knew how to make it work in landscape.
OK. You need to specifically force the orientation. try something like this.
Thanks, I'll try and take a look over the weekend.
Anyone know what are the chances of Apple approving your app if you use some of these undocumented features?
I'm guessing you'd never get listed in App Store?
Has anyone found out if apple will approve your app using this stuff? I realise this was posted a while ago, but would like to know if it is worth my time using it :)
Replies
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIf you don't tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards. That took a little messing with to get things straightened out. Then, to access the value that was entered into the text field when the ok button is clicked, you just do something like this. In this particular example, myString is an iVar that's already been alloc'ed and init'ed. Thus far, I've not figured out a way to make the text field return if the user presses the return button on the keyboard. I've tried all kinds of things and nothing that I've tried works.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {switch(buttonIndex) {
case 0:
[myString setText:@\"Cancel button pressed\"];
break;
case 1:
[myString setText:[[alertView textField] text]];
break;
}
}
Just so you know, this will compile and you will get some warnings saying that UIAlertView may not respond to the messages, but it will.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeLike the "select available WiFi network" popup?
I'd like to try and implement that if possible.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAnyone know what are the chances of Apple approving your app if you use some of these undocumented features?
I'm guessing you'd never get listed in App Store?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome[LIST=1]
[*] Add the UITextFieldDelegate protocol to your controller.
[*] Set the delegate of the UIAlertView's text field to self.
[*] Add the following function:
[/LIST]
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[(UIAlertView *)[textField superview] dismissWithClickedButtonIndex:1 animated:YES];
return NO;
}
This will intercept the return key press and use it to dismiss the alert with the "OK" button pressed.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI've tried to catch the returning value, but the delegate method doesnt get called. 1. UIViewController
2. [[myAlert textField] delegate:self]; ???
3. I added the function
I am sure that Im not setting the delegate correctly. In the debugger, I dont see a delegate item for the textField.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYou have to add these methods:
Preparation:
Show the alert:
- (void) presentSheet {
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@\"New Alert\" message:@\"your message\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Ok\", nil];
[myAlert addTextFieldWithValue:nil label:@\"I'm the Placeholder\"];
[[myAlert textField] setDelegate:self];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
}
Get clicked button and string:
Done button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@\"Dismiss Return\");
[self alertView:(UIAlertView *)[textField superview] clickedButtonAtIndex:1];
[(UIAlertView *)[textField superview] dismissWithClickedButtonIndex:1 animated:NO];
return NO;
}
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIt should work fine.
Look at those lines:
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNote that the sample project doesn’t demonstrate the "Leaks fixes it" randomness.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeMEGAUPLOAD - The leading online storage and file delivery service
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeOh, and when I press a button on the keyboard while STILL in landscape, the pop-up that shows what key you are pressing comes up where the key would be if it was landscape :S.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThis thread is about adding text fields to alerts.
I have an alert with a text field, and I was wondering if anyone knew how to make it work in landscape.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHi, I'm getting the same problem,
I have 3 text fields and I did this
[[alert textFieldAtIndex:0] becomeFirstResponder];
[[alert textFieldAtIndex:1] becomeFirstResponder];
[[alert textFieldAtIndex:2] becomeFirstResponder];
Also tried just this:
[[myAlert textField] becomeFirstResponder];
To really see the issue, use a numberic keyboard type:
af = [alert textFieldAtIndex:1];
af.clearButtonMode = UITextFieldViewModeWhileEditing;
af.keyboardType = UIKeyboardTypeNumberPad;
af.keyboardAppearance = UIKeyboardAppearanceAlert;
af.autocorrectionType = UITextAutocorrectionTypeNo;
Has anyone figured this out yet? Thanks so much for the help!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome