How to move the buttons in a UIAlertView to make r

2020-02-17 04:34发布

[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 exactly what I want, and wrap it in a view controller and presentModalView -- but I was sort of hoping that there was a standard, normal user-input "dialog" view that came-with Cocoa-touch. "Enter your name", "enter text to search", etc., are VERY common things!

Anyway... here's the question as I originally asked it:

This code:

UIAlertView* find = [[UIAlertView alloc] init];
[find setDelegate:self];
[find setTitle:@"Find"];

[find addButtonWithTitle:@"Cancel"];
[find addButtonWithTitle:@"Find & Bring"];
[find addButtonWithTitle:@"Find & Go"];
[find addButtonWithTitle:@"Go To Next"];

[find addSubview:_findText];

CGRect frm = find.frame;
int height = frm.size.height + _findText.frame.size.height + 100; // note how even 100 has no effect.
[find setFrame:CGRectMake(frm.origin.x, frm.origin.y, frm.size.width, height)];

[find setNeedsLayout];
[find show];
[find release];

Produces this Alert view:

Find Alert http://www.publicplayground.com/IMGs/Misc/FindAlert.png

(I started with the code from this question by emi1Faber, and it works as advertised; however, as I state in my comment, the cancel button overlays the text field.)

How do I reshuffle everything to make the text field fit properly? [findAlert setNeedsLayout] doesn't seem to do anything, even after I [findAlert setFrame:tallerFrame]. Hints?

Thanks!

标签: cocoa-touch
8条回答
趁早两清
2楼-- · 2020-02-17 05:00

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.

查看更多
成全新的幸福
3楼-- · 2020-02-17 05:08

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.

查看更多
登录 后发表回答