I have an array of strings that I want to use for button titles on a UIActionSheet. Unfortunately, the otherButtonTitles: argument in the method invocation takes a variable length list of strings, not an array.
So how I can I pass these titles into the UIActionSheet? The workaround I've seen suggested is to pass nil into otherButtonTitles:, then specify the button titles individually by using addButtonWithTitle:. But this has the problem of moving the "Cancel" button to the first position on the UIActionSheet rather than the last; I want it to be the last one.
Is there a way to 1) pass an array in lieu of a variable list of strings, or alternatively 2) move the cancel button to the bottom of the UIActionSheet?
Thanks.
I got this to work (you just need to, be ok with a regular button, and just add it after :
NSArray *array = #[#"1st Button",#"2nd Button",#"3rd Button",#"4th Button"];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title Here"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// ObjC Fast Enumeration
for (NSString *title in array) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
One little note: [actionSheet addButtonWithTitle:] returns the index of that button, so to be safe and "clean" you can do this:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
Taking Jaba's and Nick's answers and extending them a little further. To incorporate a destruction button into this solution:
// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
[actionSheet addButtonWithTitle: actionName];
}
// Destruction Button
if (destructiveName.length > 0){
[actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}
// Cancel Button
[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle:#"Cancel"]];
// Present Action Sheet
[actionSheet showInView: self.view];
There is the swift version for the response :
//array with button titles
private var values = ["Value 1", "Value 2", "Value 3"]
//create action sheet
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
//for each value in array
for value in values{
//add a button
actionSheet.addButtonWithTitle(value as String)
}
//display action sheet
actionSheet.showInView(self.view)
To get value selected, add delegate to your ViewController :
class MyViewController: UIViewController, UIActionSheetDelegate
And implement the method "clickedButtonAtIndex"
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
let selectedValue : String = values[buttonIndex]
}
Related
I have a method which throws up an actionSheet which is populated by an array.
The problem is that as soon as I place that method in the sender for 1 button, it fires off for every single button press.
I only want it to display for the one button (btnPortfolio).
Here is the code for my buttons:
- (IBAction)btnPortfolio:(id)sender {
[self populatePortfolioList];
}
- (IBAction)btnAdd:(id)sender {
}
- (IBAction)btnRefresh:(id)sender {
}
and here is my method:
-(void)populatePortfolioList{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose your option"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *title in portfolio_list) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
since you are using IBActions, I assume that you have somehow created the methods by ctrl+dragging from the buttons into your code? is that the case? it might be possible that you accidentally connected more than one of your buttons with the method btnPortfolio which causes populatePortfolioList to be called any time the button is pressed
Hi I am facing very different kind of problem. I am using actionsheet in my iPhone application. So when I use predefine values of otherButtonTitles and if I try to change the text size then it works fine. But when I add buttons dynamically then it's not working properly.
My code looks like this:
- (void)showActions
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:nil
otherButtonTitles:notifications, about, faq, settings, logout, nil];
[actionSheet showInView:self.view];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)_currentView;
button.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_16];
}
}
}
So above thing working fine. But if I try following things its not working
-(void) showCategories
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// categoryData is string array....
for (NSString *category in categoryData) {
[actionSheet addButtonWithTitle:category];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
And I tried to change text size like above then its not working. Is there any reason? Need help. Thank you.
When total number of buttons exceeds height to display without
scrolling this problem occurs. (If anyone else finds the way then
please post an answer. But i think there's no answer to this !!)
On 4 inch device (640x1136 resolution), number of max buttons displayed are 11
On 3.5 inch device (640x960 resolution), number of max buttons displayed are 8.
A piece of advice:: I think if you use actionsheet this way to display more than 10 elements, Apple could reject your app. Use UIPickerView for this kind of behaviour.
I have an IF statement that checks labels to see if a label is empty, if it is show an alert.
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
later on in the function I have perform segue, like this :
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
True, the alert does fire when the label is blank, but it also performs the segue. This perform segue line is not in the IF statement. So I would have thought it would have ran the IF statement and stayed there till I pressed OK. OK would be staying on the same view controller.
The segue is performed, is this due to Blocks ? any advice ?
So if the USER pressed Ok from the UIAlert the VC does not move, it stays where it was so the user can enter the details required.
This is my code :
- (IBAction)SettingsSave:(id)sender {
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
more code...
then at the end
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
}
thanks
Seems to be an if/else logic issue. Read about it.
-(IBAction)yourAction:(id)sender
{
if ([_DOBDate.text length] == 0)
{
//Show your AlertView as you did
}
else
{
//put the rest of your code, including the performSegue
}
}
UIAlertView doesn't block execution. It runs asynchronously. Therefore the code path will go into your if statement, then continue past it.
If you only want the segue to be performed after the user presses the alert view button then you need to implement UIAlertViewDelegate.
In your header file add something like this:
#interface MyController : UIViewController <UIAlertViewDelegate>
When you create the alert view do it like this:
UIAlertView *message = [[UIAlertView alloc] initWithTitle: #"Cannot Proceed"
message: #"Please Submit your DOB and Gender"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[message show];
And add this method to implement the UIAlertViewDelegate.
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex {
// perform segue here. You can also check what button was pressed based on the button index.
}
In UIActionSheet i have a uitableview and cancel button. By default cancel button appears at the top. I want to make it appear at the bottom after tableview. How do i do that?
it is work for me:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"SomeTitle" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionSheet addButtonWithTitle:#"Some Action"];
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons -1;
I tried this and Cancel button is at the bottom:
menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"destructive"
otherButtonTitles:#"other1", nil];
menu.actionSheetStyle = UIActionSheetStyleDefault;
[menu addButtonWithTitle:#"Cancel"];
By default Cancel button is hidden, adding a Cancel will shows it.
BUT: if you have additional gui elements on your actionsheet you have to
option1) position those to hide other buttons (to have space for your gui element). Its a tweak, but can work in some situation OR
option2) you have to manually add your button to the actionsheet
Actionsheet's built-in buttons are not align-able to the bottom freely because the purpose is different for this built-in gui elements.
See this: Adding UIPickerView to UIActionSheet (buttons at the bottom)
Example in Swift:
func presentMyActionSheetIOS7() {
let actionSheet: UIActionSheet = UIActionSheet(title: "What do you want to do?", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
actionSheet.addButtonWithTitle("Change Transparency")
actionSheet.addButtonWithTitle("Hide Photo")
actionSheet.addButtonWithTitle("Cancel")
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1
actionSheet.showInView(self.view)
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
println("Change Transparency")
case 1:
println("Hide Photo")
case 2:
println("Cancel")
default:
println("Default")
}
}
So I'm completely drawing a blank on the name of the class that draws one of these modal prompts with multiple options. I've attached a screenshot. It's the prompt that's confirming whether or not the user wants to sign out.
That's a UIActionSheet, Read UIActionSheet Class Reference.
Example
In your header, you need to add the <UIActionSheetDelegate> protocol. Then in your #implementation this is how you would call this specific sheet:
UIActionSheet * sampleSheet = [[UIActionSheet alloc] initWithTitle:#"Are you sure you'd like to sign out from Path?" delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Sign Out" otherButtonTitles:nil];
[sampleSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[sampleSheet showInView:self.view];
And this is how you would handle the sheet:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch ( buttonIndex )
{
case 0:
NSLog(#"User Chose Cancel");
break;
case 1:
NSLog(#"User Chose to Sign out.");
break;
}
}
You want UIActionSheet Class Reference
See UIActionSheet, I think that's the class you're looking for.
UIActionSheet *popupQuery;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//give the user an option to choose an image from their library or take a new photo...
popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose From Library", nil];
popupQuery.tag = 0;
}
else {
//give the user an option to choose an image from their library or take a new photo...
popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Choose From Library", nil];
popupQuery.tag = 1;
}
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:[self.view window]];
[popupQuery release];