开发者

Add elements to UIAlertview

i need to insert a view (a custom button) under the 2 default b开发者_如何学Cuttons of alert view in iphone project. I search a long but i don't find how insert element under the buttons, tips?


Not exactly clear what you're trying to do, but there are a few possibilities. If you just want regular buttons, you can stick as many of them as you want on there with the otherButtons parameter.

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other", @"Other2", nil];

You can also use addButtonWithTitle to add as many as you want.

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"OK", nil];
[anAlert addButtonWithTitle:@"My Other Button"];

If you really want a custom button, things get trickier. Jamming arbitrary views into UIAlertViews is somewhat of a hobby of mine (see my blog posts on the topic here, and here, and here). However, in all those cases the views end up above the normal buttons; the buttons are anchored to the bottom of the view, and I know of no way to change that behavior.

One thing you could do is create the UIAlertView with no buttons, and then create and add as many custom buttons as you want. First, I'd like to point out that this is probably a bad idea. It's not going to be a good user experience, and unless you're TapBots, sticking with Apple's HIG is usually a good idea.

Having said all that, something like this might work:

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

UIButton* okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
okButton.frame = CGRectMake(12, 80, 130, 50);
[okButton setTitle:@"OK" forState:UIControlStateNormal];
[anAlert addSubview:okButton];

UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.frame = CGRectMake(144, 80, 130, 50);
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[anAlert addSubview:cancelButton];

UIButton* otherButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
otherButton.frame = CGRectMake(12, 136, 260, 50);
[otherButton setTitle:@"My Other Button" forState:UIControlStateNormal];
[anAlert addSubview:otherButton];   

The linefeeds at the end of the alert message expand the alert view horizontally, making room to add your buttons. Here I'm just doing plain UIButtons (which looks dreadful); hopefully your custom buttons look nicer. You need to lay them out yourself by hardcoding the coordinates, which is brittle (and changes depending on your message, and the screen orientation). Finally, since there aren't any default buttons, there isn't a way to dismiss the alert, so you'd have to make sure to stick handlers on these buttons to make that happen. You could do this with addTarget:action:forControlEvents: on the buttons.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜