Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
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.

New info on adding text fields to alerts

myersn024myersn024 Posts: 315Tutorial Authors
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.
Post edited by myersn024 on
«1

Replies

  • StatCoderStatCoder Posts: 221Registered Users
    I'd be interested in seeing what this looks like.
    STAT ICD-9 LITE|<a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291155883&mt=8" target="_blank
  • myersn024myersn024 Posts: 315Tutorial Authors
    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.

    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@\"Alert title\" message:@\"alert message\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Ok\", nil];
    [myAlert addTextFieldWithValue:nil label:@\"<place holder>\"];
    [[myAlert textField] setTextAlignment:UITextAlignmentCenter];
    [[myAlert textField] becomeFirstResponder];
    [myAlert show];
    [myAlert release];
    myAlert = nil;


    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.

    - (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.
  • StitchStitch Posts: 401Registered Users
    Was there anything in there about adding a small table view to an alert?

    Like the "select available WiFi network" popup?

    I'd like to try and implement that if possible.
    BUZZER! : iTunes Library Music Quiz (1 or 2 Player)
  • myersn024myersn024 Posts: 315Tutorial Authors
    No, but there's some undocumented stuff in UIApplication that I think is for that purpose. I haven't messed around with it, though.
  • StitchStitch Posts: 401Registered Users
    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?
    BUZZER! : iTunes Library Music Quiz (1 or 2 Player)
  • frankusfrankus Posts: 1New Users
    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]
    - (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.
  • giacomop81giacomop81 Posts: 7New Users
    myersn024;14701 said:
    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.

    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@\"Alert title\" message:@\"alert message\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Ok\", nil];
    [myAlert addTextFieldWithValue:nil label:@\"<place holder>\"];
    [[myAlert textField] setTextAlignment:UITextAlignmentCenter];
    [[myAlert textField] becomeFirstResponder];
    [myAlert show];
    [myAlert release];
    myAlert = nil;


    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.

    - (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.
    I found it very usefull, but now in need to get the string in the editField. How can i get it?
    Thanks
  • turbolagturbolag Posts: 32Registered Users
    Great work!

    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.
  • ForswornForsworn Posts: 504New Users
    I got it!

    You have to add these methods:

    Preparation:
    <UITextFieldDelegate> //import the Delegate protocol
    - (void) presentSheet; //declare method in header file


    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:
    - (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
    }
    }
    }


    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;
    }
  • rendezvouscprendezvouscp Posts: 53Registered Users
    myersn024;14701 said:
    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.
    I’m still trying to work this one out. I’m 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.
  • ForswornForsworn Posts: 504New Users
    Did you use the code I posted before?
    It should work fine.

    Look at those lines:
    	[[myAlert textField] becomeFirstResponder];
    [myAlert show];
  • rendezvouscprendezvouscp Posts: 53Registered Users
    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.
  • ForswornForsworn Posts: 504New Users
  • rendezvouscprendezvouscp Posts: 53Registered Users
    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.
    AlertViewWithTextField.zip
    1 x 1 - 15K
  • rendezvouscprendezvouscp Posts: 53Registered Users
    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.
  • xspykxspyk Posts: 30Registered Users
    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.
  • wuf810wuf810 Posts: 1,052Registered Users
    xspyk;36220 said:
    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.
  • xspykxspyk Posts: 30Registered Users
    wuf810;36227 said:
    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.
  • flipconversionflipconversion Posts: 83Registered Users
  • wuf810wuf810 Posts: 1,052Registered Users
    xspyk;38722 said:
    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.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
  • HoofSCHoofSC Posts: 41Registered Users
    Can anyone specify whether or not you can somehow grab the value of user input in TWO UITextFields added to the alertView (in Succession): ????


    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@\"Input New Values\" message:@\"Enter new values\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Record New Values\", nil];

    [myAlertView addTextFieldWithValue:nil label:@\"<Value 1 Placeholder>\"];
    [myAlertView addTextFieldWithValue:nil label:@\"<Value 2 Placeholder>\"];

    [[myAlertView textField] becomeFirstResponder];
    [myAlertView show];
    [myAlertView release];
  • rossmclachlanrossmclachlan Posts: 4New Users
    Stitch;14903 said:
    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 :)
  • nobre84nobre84 Posts: 963Registered Users
    HoofSC;42331 said:
    Can anyone specify whether or not you can somehow grab the value of user input in TWO UITextFields added to the alertView (in Succession): ????


    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@\"Input New Values\" message:@\"Enter new values\" delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Record New Values\", nil];

    [myAlertView addTextFieldWithValue:nil label:@\"<Value 1 Placeholder>\"];
    [myAlertView addTextFieldWithValue:nil label:@\"<Value 2 Placeholder>\"];

    [[myAlertView textField] becomeFirstResponder];
    [myAlertView show];
    [myAlertView release];
    Hi, grab each individual text field with [myAlertView textFieldAtIndex:0]
  • pawpaw17pawpaw17 Posts: 7New Users
    ----------
    Forsworn;30333 said:
    Did you use the code I posted before?
    It should work fine.

    Look at those lines:
    	[[myAlert textField] becomeFirstResponder];
    [myAlert show];
    -------------

    Hi, 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!
Sign In or Register to comment.