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
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 want to present an alert view after the user click on a button at 2s. The function is to start calibration, and I want to show the user that calibration is done after 2s.
I'm counting in an array that when it reaches 20 (presents 2s), and I used
if showCalibDone {
let calibDoneAlert = UIAlertController(title: "", message: "Calibration is finished", preferredStyle: .Alert)
calibDoneAlert.addAction(UIAlertAction(title: "", style: .Default, handler: {(action: UIAlertAction!) in
self.x0 = self.average40(self.xCalibrate)
self.presentViewController(calibDoneAlert, animated: true, completion: nil)
}))
This is the counting. xCalibrate is an array which will add an object each time the accelerometer updates.
if xCalibrate.count == 40 {
println("40 achieved")
self.showCalibDone = true
}
But the alert view never appears with this if condition. I tried to put it in viewWillAppear or viewDidLoad, but it seems both are not correct.
How can I get this 'loop' be executed? Where should I put it? Or maybe I should use a timer to count?
If you just want some code to be executed after some a delay, use dispatch_after:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue()) {
// ...
}
You can also use performSelector to achieve the same results
- (void) showAlert
{
[[[UIAlertView alloc] initWithTitle:#"Title" message:#"Some message" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK",nil] show];
}
//
[self performSelector:#selector(showAlert) withObject:nil afterDelay:2.0f];
UIAlertController with two buttons with styles set:
UIAlertActionStyle.Cancel
UIAlertActionStyle.Default
in iOS 8.2, the Cancel button is non-bold and Default is bold.
In iOS 8.3 they have switched round
You can see it Apple's own apps e.g., Settings > Mail > Add Account > iCloud > enter invalid data, then it shows like this on 8.3:
Unsupported Apple ID
Learn More (bold)
OK (non-bold)
whereas it was the other way round for 8.2.
Any workaround to make it like 8.2 again. Why has it changed?
From iOS 9 you can set the preferredAction value to the action which you want the button title to be bold.
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(cancelAction)
alert.addAction(OKAction)
alert.preferredAction = OKAction
presentViewController(alert, animated: true) {}
The OK button which is on the right will be in bold font.
This is an intentional change to the SDK. I have just had a response from Apple to this radar on the issue, stating that:
This is an intentional change - the cancel button is to be bolded in alerts.
I can't find anything in the various change logs mentioning this, unfortunately.
So, we'll need to make changes to our apps in places to make some things make sense.
Since iOS 9, UIAlertController has a property called preferredAction. preferredAction has the following declaration:
var preferredAction: UIAlertAction? { get set }
The preferred action for the user to take from an alert. [...] The preferred action is relevant for the UIAlertController.Style.alert style only; it is not used by action sheets. When you specify a preferred action, the alert controller highlights the text of that action to give it emphasis. (If the alert also contains a cancel button, the preferred action receives the highlighting instead of the cancel button.) [...] The default value of this property is nil.
The Swift 5 / iOS 12 sample code below shows how to display a UIAlertController that will highlight the text of a specified UIAlertAction using preferredAction:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { action in
print("Hello")
})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
alertController.preferredAction = okAction
present(alertController, animated: true, completion: nil)
I just checked in iOS 8.2: a first added button is non-bold and a second added button is bold. With this code a cancel button will be bold:
[alertController addAction:[UIAlertAction actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil]];
And with this code a default button will be bold:
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:nil]];
I can't check in iOS 8.3 now but this behavior can be a reason.
Some words about objective-c and preferredAction for alertActions. If you use preferredAction you BOUTH alertAction must set as style:UIAlertActionStyleDefault. If some one will be set as style:UIAlertActionStyleCancel, preferredAction will be ignored
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"All you base"
message:#"Are belong to us!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alertActionShowYes = [UIAlertAction actionWithTitle:#"YES!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"I serve for my emperor!");
}];
UIAlertAction* alertActionNo = [UIAlertAction actionWithTitle:#"NO!" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"NOOOO it's not true!!");
}];
[alertController addAction:alertActionShowYes];
[alertController addAction:alertActionNo];
alertController.preferredAction = alertActionShowYes;
[alertController setPreferredAction:alertActionShowYes];
[self presentViewController:alertController animated:YES completion:nil];
Versions of iOS prior to 8 allowed me to create a UIActionsheet which would show a group of buttons, some space, and then a cancel button. Something like this:
However in iOS 8 when I try and create the same look I end up with something that looks like this:
The code, in iOS 8 looks like this:
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC.view setTintColor:[UIColor copperColor]];
UIAlertAction* notifyViaPush = [UIAlertAction
actionWithTitle:#"Send Alert to my phone"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* notifyViaEmail = [UIAlertAction
actionWithTitle:#"Notify me by email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
[alertVC addAction:notifyViaPush];
[alertVC addAction:notifyViaEmail];
[alertVC addAction:cancel];
[self presentViewController:alertVC animated:YES completion:nil];
How can I group my buttons and have some space between the actions and cancel button using UIAlertController?
The line alertVC.view.tintColor = [UIColor copperColor]; is causing problem, it makes the whole view of alert controller the same color, in your first picture the cancel button has white background. To fix this, move this line to the end of the function, that is, after you have added all actions.
For me it worked when I set the style to UIAlertActionStyleCancel on the UIAlertAction.
In the code below, action is a UIAlertAction object and controller is a UIAlertController with style Action sheet.
UIAlertAction *action2 = [UIAlertAction actionWithTitle:#"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
self.tabBarView.selectedIndex = 1;
[self.tabBarView setSelectedIndex:self.tabBarView.selectedIndex];
}];
[controller addAction:action];
[controller addAction:action2];
Swift Answer
Set the style: parameter of the action button that you want to separate to .cancel instead of .default.
like this style: .cancel
or more specifically this
// **style: on this is set to .cancel**
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }
It's usually the button that you named "Cancel"
I'm not sure about how setting the button text colors the way the op said he did it in his answer affects the way the actions get separated. I use the actions "titleTextColor" to change the text color and it has no effect on the spacing the op is asking about.
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
Here's the code in 4 steps
func presentActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// 1. style: is set to .default on both of these which will keep them grouped
let blockAction = UIAlertAction(title: "Block", style: .default) { (action) in }
let reportAction = UIAlertAction(title: "Report", style: .default) { (action) in }
// 2. style: is set to .cancel which will separate it from the other two actions
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in }
// 3. set the colors for the action text
blockAction.setValue(UIColor.brown, forKey: "titleTextColor")
reportAction.setValue(UIColor.purple, forKey: "titleTextColor")
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
// 4. add the buttons to the action sheet and make sure the cancel button is last
actionSheet.addAction(blockAction)
actionSheet.addAction(reportAction)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I created a UIAlertView with its alertViewStyle set to UIAlertViewStylePlainTextInput.
I want to change the keyboard type, but not by adding a subview.
My Codeļ¼
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles:#"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
If You want to present the Input Style, First of all implement the UIAlertViewDelegate in your class.
Secondly when you presenting the alertView set the delegate to self.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"a" message:#"b" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"aaa", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];'
If you want to change the keyboard type for particular field then do like this
e.g for 1st field, it will make the keyboard numeritc.
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];
UPDATE for iOS 8.0 Swift
From ios 8.0 onward UIAlertView is deprecated so you might need to use UIAlertController
var alert = UIAlertController(title: "Title", message: "Your msg",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel,
handler:yourHandler))
alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Password"
textField.secureTextEntry = true // setting the secured text for using password
textField.keyboardType = UIKeyboardType.Default
})