How to move the buttons in a UIAlertView to make room for an inserted UITextField?

后端 未结 8 707
挽巷
挽巷 2020-12-24 00:07

[EDIT] Hmm. Perhaps this question should be titled \"what is the default user-input dialog view called in CocoaTouch?\" I realize that I can create an entire view that is

相关标签:
8条回答
  • 2020-12-24 00:16

    Most probably You would want to look into the addTextFieldWithValue method of the UIAlertView? Add the following code somewhere at the top of Your class:

    @interface UIAlertView ()
    - (void) addTextFieldWithValue: (NSString*) val label: (NSString*) label;
    - (UITextField*) textField;
    @end
    

    It’s not official, but IMHO it’s not getting You rejected from the App store and it’s much better solution than hacking the textfield into the dialog Yourself.

    0 讨论(0)
  • 2020-12-24 00:16

    https://github.com/TomSwift/TSAlertView

    This library actually creates the control from scratch rather than attempting to hack UIAlertView, which is generally a Bad Plan (TM)

    0 讨论(0)
  • 2020-12-24 00:24

    Zoul proposed the best method, to capture user input just do:

    a) Add the UITextFieldDelegate protocol to your class.

    b) Do something like

        UIAlertView *insertScore = [UIAlertView new];
        [insertScore setDelegate:self];
        [insertScore setTitle:@"New Title!"];
        [insertScore addButtonWithTitle:@"Cancel"];
        [insertScore addButtonWithTitle:@"Ok"];
    
        insertScore.message = @"\n";
    
        [insertScore addTextFieldWithValue:@"Input" label:@"player"];
    
        [[insertScore textField] setDelegate:self];
    
        [insertScore show]; 
    
        [insertScore release];
    

    c) The crucial part was to set the delegate of the textField to self, then to access data you can simply:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
      NSLog(@"%@",[[alertView textField] text]);
    }
    

    Hope this helps someone, since I had to think a bit to get it right.

    0 讨论(0)
  • 2020-12-24 00:25

    Try putting in some (\n)s after the title in the UIAlertView initialization. That will push down the buttons. And I agree with Stephen here. There are chances that Apple might reject an app if it uses controls in a way they shouldn't be. (there's some clause in the Human Interface Guidelines about that!)

    0 讨论(0)
  • 2020-12-24 00:31

    Even if you can get this working it's not going to be very iPhone-y. The UIAlertView really is not designed for user input like this. If you look in all the Apple apps you'll see that they use a new view that displayed using the presentModalViewController: method of UIViewController.

    Edit: This advice is no longer as true as it was when I wrote it. Apple have increasingly used alert views as text entry boxes and iOS5 even includes native support without having to mess around with views (check out the alertViewStyle property).

    I think maybe if you need to have four buttons then using a custom UIViewController is probably still the right way to go. But if you just want to enter a password with OK/Cancel buttons then it's fine.

    0 讨论(0)
  • 2020-12-24 00:34

    This simpler method works for me:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
        message:@"<Alert message>" delegate:self cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alert addTextFieldWithValue:@"" label:@"Text Field"];
    

    Hope that helps. Oh if you needed multiple button rows then it's:

    [alert setNumberOfRows:3];
    

    Cheers

    0 讨论(0)
提交回复
热议问题