Drop-down menu in iOS alert - iphone

I'm trying to set labels to certain areas of an app dynamically so that some of them show while others are hidden. I have an alert that prompts for text to fill these labels with, but I know the options in advance and would like to only allow the user to choose certain options.
Is there a way to have a drop-down menu of options in an iOS alert rather than a simple text input? Any help would be appreciated greatly.

You can't have a dropdown menu but you can have multiple buttons, one for every option.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"title" message: #"message"
delegate: self cancelButtonTitle: #"Cancel" otherButtonTitles: #[#"option1", #"option2",
#"option3"], nil];

You don't need:
#[#"option1", #"option2", #"option3"], nil
Use simple strings instead:
#"option1", #"option2", #"option3", nil

Related

Highlight Top Button in UIAlertView

I've got a UIAlertView with 3 buttons displayed vertically by default in the UIAlertView. I'd like the top button to be bold/highlighted. From my understanding and testing, the 'cancel' button is the one that is highlighted. The problem is no matter how I set the cancel button, it is placed last in this row. I cannot get it to be the first button.
I've tried setting the cancel button explicitly
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:#"Top Button"
otherButtonTitles:#"Middle Button", #"Bottom Button", nil];
as well as setting the index of the cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Top Button", #"Middle Button", #"Bottom Button", nil];
alert.cancelButtonIndex = 0;
This problem is actually caused by changes Apple made in iOS 7. Prior to iOS 7 we were able to access the subviews of an UIAlertView by calling [alertView subviews]. But since iOS 7 doesn't give us access to any subviews ([alertView subviews].count will always return zero) we can't customize UIAlertViews the way we used to.
So the only way to achive your goal under iOS 7 is to build a custom view that looks like UIAlertView and then customize it as you like.
But if you're coding for an iOS version prior to iOS 7 than you could use this easy hack to access a button:
UIAlertView *alertView = [[UIAlertView alloc] init];
[alertView addButtonWithTitle:#"Yes"];
UIButton *yesButton = [alertView.subviews lastObject]; //is nil under iOS 7
This way you would get access to the first button. After that you can customize your UIAlertView as usual.
By the way: Apple did not only want to give all UIAlertViews the same design by changing the way we can customize them. The reason lies in HCI researches (Human-Computer-Interaction). People tend to think the bottom button is always the 'default' answer if that is the way it is implemented throughout all apps.
Also the bottom button is the only highlighted button in a UIAlertView. So its visual weight is stronger than the visual weight of the button with about the same amount of text. That's another factor why people tend to choose this one. And that is also the reason why the highlighted button never should cause disastrous and irreversible actions ('You wanna delete all your saved games' should always highlight the button 'Keep my saved games' and not the one telling 'Delete everything').
Therefore Apple always makes the Cancel Button the bottom one no matter in which order you added the buttons. So if your app doesn't make use of a fully custom interface and uses many User Interface Elements provided by Apple than I highly recommend you to not try to change that behavior and make the bottom button your 'default' button.
There is a customer alert view DTAlertView.
I hope it can help you.

How to set identical appeareance to buttons for an UIAlertView?

By default, a UIAlertView with two buttons has different alpha for each button.
Is there a way to make them look identical with different text, without subclassing UIAlertView, or making a custom alert view?
Here is what i used:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
kAlertTitleOrderType message:kAlertMessageOrderType delegate:delegate
cancelButtonTitle:nil otherButtonTitles:#"Collection", #"Delivery", nil];
If you set the cancel button to nil when calling initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: then no cancel button will be added (this is the one that looks different). You can then add other buttons as you require.

UIAlertView, how to place a lot of buttons inside it?

In UIAlertView, how can I place a lot of buttons inside it?
I've got an alert view and want to place ten buttons inside of it, but when in place, then they don't fit in the screen?
Moreover, how should I make the buttons look a little bit different, and how can I change their sizes?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Фильтр по категориям" message:#"Выбирите категорию:" delegate:self cancelButtonTitle:#"Отменить"
otherButtonTitles:#"Все", #"Избранное", #"Тосты", #"Рассказы", #"Стихи", #"Анекдоты", #"Афоризмы", #"Объявления", #"Разное", nil];
In English:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: # "Filter by Category" message: # "Choose a category:" delegate: self cancelButtonTitle: # "Cancel"
otherButtonTitles: # "All", # "Music", # "Toast", # "Stories", # "Poems", # "Jokes", # "Aphorisms", # "Announcements", # "Other", nil];
You should not place ten buttons inside a UIAlertView.
I don't know Russian so I have no clue as to the context of your problem. But there's no reason to put that many choices in an alert view.
Lorean is correct.
On the iPhone, for selecting a category filter, you should use a modal ViewController (with presentModalViewController), which will roll over your main view, allow them to select their category and then disappear.
On the iPad, you should use a UIPopoverController.
You might want to look on alternative alert view implementations https://github.com/TomSwift/TSAlertView
But be honest to yourself, if so many buttons make sense on a alert view. You also can use presentModalViewController to present a custom view, where you have all flexibility.

change position of cancel button in UIAlertView?

I noticed that when I delete an app from my iPhone home screen, the alert view that appears shows a Delete button on the left and Cancel on the right. However, when I build a delete function within my app using UIAlertView, the buttons only seem to display with Cancel on the left and Delete on the right.
I'd like my app to be consistent with the OS, but I can't figure out how to make the Cancel button appear first. Does anyone know?
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Delete Song"
message:#"Are you sure you want to delete this song? This will permanently remove it from your database."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
I tried setting alert.cancelButtonIndex = 1, but that had no effect.
Ah, I just figured out how to change this. The cancelButtonTitle argument is optional, so you can add a custom button in whatever position you want and then designate that as the cancel button, like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Delete Song"
message:#"Are you sure you want to delete this song? This will permanently remove it from your database."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Delete", #"Cancel", nil];
alert.cancelButtonIndex = 1;
That puts the Delete button the left and the Cancel button on the right and highlights the Cancel button.
A possible reason Apple used an alert view on the home screen was because it once asked users to rate the apps they were removing (not anymore). They likely made the Cancel button the lighter-colored one because this was considered a destructive action (deletes an app and its data).
I guess you could reverse the titles (cancelButtonTitle:#"Delete" otherButtonTitles:#"Cancel", nil) and handle clicks on those buttons the other way around (not sure if Apple did the same). That would be a little awkward though; how about using an action sheet instead?

What class handles the popup/notification windows on iphone?

I'm looking for the class name of the popup/message windows on the iPhone (it's a blueish window that comes up when you have a missed call, or a message comes in for example.)
The class is called UIAlertView. From the documentation:
Use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).
Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the UIAlertViewDelegate protocol. Use the show method to display an alert view once it is configured.
The examples you gave (missed call or incoming text message) are system level alerts that pop up over any application. That functionality is not available through the SDK. lajos's answer does provide the correct way to display an alert, but it is worth remembering you can only do this within your application. You cannot pop up an alert over another app because the SDK currently prohibits an app from running in the background.
Further to this response, UIAlertView is indeed the way to do this and the code you want is:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Message" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert show];
Here the alert box will popup with the message "Message" and have a single button titled "OK" which will close the popup when clicked. Check the documentation for other things you can do (more buttons etc).