How can I build a choice view something like the image below, or the stock picker from Apple for example.
I know I can make the 'normal' UIPickerView but this looks much better.
Try this:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil delegate: nil cancelButtonTitle: #"Cancel" destructiveButtonTitle: nil otherButtonTitles: #"Take photo or video", #"Choose photo",… nil];
[actionSheet showInView: self.view);
or in Swift
UIActionSheet(title: nil,
delegate: nil,
cancelButtonTitle: "Cancel",
destructiveButtonTitle: nil,
otherButtonTitles: "Take photo or video", "Choose photo").showInView(self.view)
Related
This is the code I am writing to display UIActionSheet.
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"updateresponseforrecurring", nil) delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:
NSLocalizedString(#"updateresponseonlyforthis", nil),
NSLocalizedString(#"updateresponseforallactivities", nil),
nil];
actionSheet.tag = 2;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
This is what I get using this :
Clearly, second option is longer and thus the size gets smaller to accommodate the width.
But I want the same font size for all the options which leaves me with multiline. Also tried with UIAlertController but not able to set multiline text.
How to achieve this ?
This seems to work in iOS 10 and Swift 3.1:
UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 2
This is not possible with the standard UIAlertController or UIAlertView.
I would recommend you to make it shorter. Why don't you make an alert and type something like this:
Do you want to update the response only for this instance or for all
activities in this series.
The answers would be these:
Only this instance
All activities
In iOS 10:
If you want to apply it to all UIAlertController, you can use these lines of code:
[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setNumberOfLines:2];
[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setFont:[UIFont systemFontOfSize:9.0]];
put this in didFinishLaunchingWithOptions method of the AppDelegate.
try this
let alert = UIAlertController(title: title,
message: "you message go here",
preferredStyle:
UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
UILabel.appearance(whenContainedInInstancesOf:
[UIAlertController.self]).numberOfLines = 0
UILabel.appearance(whenContainedInInstancesOf:
[UIAlertController.self]).lineBreakMode = .byWordWrapping
I'm creating this alert view and trying to use the delegate method alertViewShouldEnableFirstOtherButton but it isn't being called.
Documentation says it is going to be deprecated in iOS9 but I'm using iOS8
var alert = UIAlertView(title: "Edit collection name", message: "", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.show()
*update as Duncan pointed out UIAlertView is deprecated in iOS8.
UIAlertView is deprecated in iOS8. Credit to #Duncan C
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")
}
}
How do I add a textfield to the alertview? I'm trying to do an app where before the app commits the editing, the user must authenticate 1st by typing his/her password on the said alertview but how can I do this? It seems like the code I've searched doesn't work anymore. Here it is:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log In" message:#"Please enter your Password" delegate:self cancelButtonTitle:#"Log In" otherButtonTitles:#"Cancel", nil];
[alert addTextFieldWithValue:#"" label:#"Password"];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocapitalizationType = UITextAutocorrectionTypeNo;
the error said
"No visible #interface for 'UIAlertView'
declares the selector 'addTextFieldWithValue:label:'"
on the [alert addTextFieldWithValue:#"" label:#"Password"];
I also would like to ask how can I put codes on the Confirm button on the alertview.
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
or if you need it to be secure (for passwords)
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Edit:
I'm not 100% sure what you mean by "put codes on the confirm button" but if you want to know whether they pushed confirm or cancel, you only need to implement
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//check the button index and do something if it's the right one.
}
Adding a textField into an alert view (Swift):
let pincodeAlert:UIAlertController = UIAlertController(title: "Hello", message: "Enter a new passcode", preferredStyle: UIAlertControllerStyle.Alert)
pincodeAlert.addTextFieldWithConfigurationHandler({ (pinCodeTextField:UITextField!) -> Void in
pinCodeTextField.placeholder = "password"
pinCodeTextField.secureTextEntry = true
})
//Add the textField
pincodeAlert.addTextFieldWithConfigurationHandler({ (pinCodeTextField2:UITextField!) -> Void in
pinCodeTextField2.placeholder = "Confirm password"
pinCodeTextField2.secureTextEntry = true
})
pincodeAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
//check entered passcodes
}))
pincodeAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
presentViewController(pincodeAlert, animated: true, completion: nil)
To check the data in the checkBoxes just add this in your "OK" Action handler:
let passcodeEntered:String = (pincodeAlert.textFields?.first as UITextField).text
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]
}