I am trying to use this code in a Pacman game I got from some website but had to change UIAlertView to UIAlertController except the following code has two errors that I don't know how to fix (I am really new to programming and feel like this is a really newbie question - sorry!!)
The first error is in line 4: No known class method for selector alertControllerWithTitle
A second error is in the last line: no visible interface declares the selector show
- (void)collisionWithExit: (UIAlertController *)alert {
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame)) {
[self.motionManager stopAccelerometerUpdates];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Congratulations"
message:#"You've won the game!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert show];
}
}
Please check the following code:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"My Alert"
message:#"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
Check below this code.
for Objective-C:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//button click event
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
for Swift 4.x:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style {
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
Swift 5: Simple Extension
extension UIViewController {
func presentAlert(withTitle title: String, message : String, actions : [String: UIAlertAction.Style], completionHandler: ((UIAlertAction) -> ())? = nil) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
for action in actions {
let action = UIAlertAction(title: action.key, style: action.value) { action in
if completionHandler != nil {
completionHandler!(action)
}
}
alertController.addAction(action)
}
self.present(alertController, animated: true, completion: nil)
}
}
Usage:
self.presentAlert(withTitle: "Network Error", message: "Please check your internet connection", actions: [
"Retry" : .default, "Cancel": .destructive] , completionHandler: {(action) in
if action.title == "Retry" {
print("tapped on Retry")
}else if action.title == "Cancel" {
print("tapped on Cancel")
}
})
OR
self.presentAlert(withTitle: "Mail services are not available", message: "Please Configure Mail On This Device", actions: ["OK" : .default] , completionHandler: nil)
Related
I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController
I have declare propery in .m
#property (nonatomic, strong) UIAlertController *alertController;
And init in loadview method
//alertviewController
_alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];
And call the alertview in viewDidLoad:
_alertController = [UIAlertController alertControllerWithTitle:#"Error display content" message:#"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];
// viewController.showNavBarBackButton = YES;
[[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController];
}];
[_alertController addAction:ok];
[self presentViewController:_alertController animated:YES completion:nil];
I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController programmatically?
- (void)logoutButtonPressed
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Logout"
message:#"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];
//Add Buttons
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
[self clearAllData];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
//Add your buttons to alert controller
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
}
In Xcode 9.4.1
Create alert function globally and use every where.
//Alert function
- (void) showAlertMsg:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message {
UIAlertController * alert = [UIAlertController alertControllerWithTitle : title
message : message
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[viewController presentViewController:alert animated:YES completion:nil];
});
}
Call like this in your required place.
Import that class and create instance
//Ex:
GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];
//Call alert function with instance
[gcvc showAlertMsg:self title:#"" message:placeholderText];
How would I create a UIAlertView in Swift?
And in
Swift >=3:
let alertController = UIAlertController(title: "Some Error",
message: "Pleas confirm?",
preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self?.present(alertController, animated: true, completion: nil)
Create global Alert controller that is accessible in all view controllers using extension.
Create an extension of UIViewController with your function to display alert with required parameter arguments.
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
Now call this function from your view controller:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}
I have two button but I just wanna change one to red.When I use the function below
it change all to red. I just want to change color of only one button. How can i do it?
alertController.view.tintColor = UIColor.redColor()
let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
alertController.setValue(NSAttributedString(string: title, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 15),NSForegroundColorAttributeName : BLACK_COLOR]), forKey: "attributedTitle")
alertController.setValue(NSAttributedString(string: message, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 13),NSForegroundColorAttributeName : APP_COLOR_BLUE_1]), forKey: "attributedMessage")
You can try this,
deleteAction.setValue(color, forKey: titleTextColor)
It works for me!
Swift
you need to use UIAlertActionStyle.Destructive for button text color in red
let alert = UIAlertController(
title: "Basic alert style",
message: "Basic alert With buttons",
preferredStyle: .alert )
let Reset = UIAlertAction(
title: "Reset",
style: .destructive) { (action) in
// do your stuff
}
let Cancel = UIAlertAction(
title: "Cancel", style: .default) { (action) in
// do your stuff
}
alert.addAction(Reset)
alert.addAction(Cancel)
present(alert, animated: true, completion: nil)
Objective-C
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:#"Basic Alert style"
message:#"Basic Alert With Buttons"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *Reset = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Reset", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Reset action");
}];
UIAlertAction *Cancel = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
[alert addAction:Reset];
[alert addAction:Cancel];
[self presentViewController:alert animated:YES completion:nil];
output
for additional Information see this
only red Color is possible when you set UIAlertActionStyle.Destructive
Check this link
UIAlertController custom font, size, color
How to create an alert like Instagram unfollow alert(two buttons, image and message) on iOS? Is there any ready component or should I develop it from scratch?
Here is a screenshot:
There is an implementation (UIAlertController), but without the image.
Here's a working example:
UIAlertController* deleteAlert = [UIAlertController alertControllerWithTitle:#"Unfollow?"
message:
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* unfollowAction = [UIAlertAction actionWithTitle:#"Unfollow" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) {
//Code to unfollow
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"Cancel", nil) style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[deleteAlert addAction:unfollowAction];
[deleteAlert addAction:cancelAction];
[self presentViewController:deleteAlert animated:YES completion:nil];
You can find more informations on how to add an image to a UIAlertController in this post:
Add Image to UIAlertAction in UIAlertController
Swift 4 version
let deleteAlert = UIAlertController(title: "Unfollow", message: "", preferredStyle: UIAlertController.Style.actionSheet)
let unfollowAction = UIAlertAction(title: "Unfollow", style: .destructive) { (action: UIAlertAction) in
// Code to unfollow
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
deleteAlert.addAction(unfollowAction)
deleteAlert.addAction(cancelAction)
self.present(deleteAlert, animated: true, completion: nil)
I want to add text input in alert-view of ios 8.
I know it was done using UIAlertController but not have any idea.
How to do it ?
Screenshot
Code
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: #"Login"
message: #"Input username and password"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"name";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"password";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = YES;
}];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
UITextField * passwordfiled = textfields[1];
NSLog(#"%#:%#",namefield.text,passwordfiled.text);
}]];
[self presentViewController:alertController animated:YES completion:nil];
AlertViewController
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"Title"
message:#"SubTitle"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(#"text was %#", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"placeHolderText";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"SubTitle"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog show];
}
Example of implementation with Swift 3:
var textField: UITextField?
// create alertController
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alertController.addTextField { (pTextField) in
pTextField.placeholder = "usefull placeholdr"
pTextField.clearButtonMode = .whileEditing
pTextField.borderStyle = .none
textField = pTextField
}
// create cancel button
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
alertController.dismiss(animated: true, completion: nil)
}))
// create Ok button
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (pAction) in
// when user taps OK, you get your value here
let inputValue = textField?.text
alertController.dismiss(animated: true, completion: nil)
}))
// show alert controller
self.present(alertController, animated: true, completion: nil)
UIAlertView *myView = [[UIAlertView alloc]initWithTitle:#"Input" message:#"Enter your value" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
myView.alertViewStyle = UIAlertViewStylePlainTextInput;
[myView textFieldAtIndex:0].delegate = self;
[myView show];
you can cover this way .Thanks
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Please enter someth" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
also, you will need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.
and if you user uialertcontroller then use this one
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Username";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Password";
textField.secureTextEntry = YES;
}];
[self presentViewController:alert animated:YES completion:nil];
Here's handy method with submit/cancel and completion handler for text if inputted:
/**
Presents an alert controller with a single text field for user input
- parameters:
- title: Alert title
- message: Alert message
- placeholder: Placeholder for textfield
- completion: Returned user input
*/
func showSubmitTextFieldAlert(title: String,
message: String,
placeholder: String,
completion: #escaping (_ userInput: String?) -> Void) {
let alertController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
alertController.addTextField { (textField) in
textField.placeholder = placeholder
textField.clearButtonMode = .whileEditing
}
let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
let userInput = alertController.textFields?.first?.text
completion(userInput)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
completion(nil)
}
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
}
This one is work for me:
let passwordString = lableGetPassword.text
var textField: UITextField?
// create alertController
let alertController = UIAlertController(title: "Password", message: "Save the password. Give a tag name.", preferredStyle: .alert)
alertController.addTextField { (pTextField) in
pTextField.placeholder = "Tag Name"
pTextField.clearButtonMode = .whileEditing
pTextField.borderStyle = .none
textField = pTextField
}
// create cancel button
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
alertController.dismiss(animated: true, completion: nil)
}))
// create Ok button
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { [self] (pAction) in
// when user taps OK, you get your value here
let name = textField?.text
save(name: name!, password: passwordString!)
alertController.dismiss(animated: true, completion: nil)
}))
// show alert controller
self.present(alertController, animated: true, completion: nil)
UIAlertViewController with text input
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// optionally configure the text field
textField.keyboardType = UIKeyboardTypeAlphabet;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textField = [alert.textFields firstObject];
textField.placeholder = #"Enter Input";
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
func askForName() {
let alert = UIAlertController(title: "Enter Text",
message: "Enter some text below",
preferredStyle: .alert)
alert.addTextField { (textField) in
textField.text = "New Text"
}
let action = UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert!.textFields![0]
print("Text field: \(textField.text)")
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
I was wondering if there were a built-in view in XCode for displaying pop-up menus like this for iOS apps? Something like an alert, except with just a set of vertically-stacked buttons?
EDIT:
I knew about UIAlertController, just not that its buttons stack vertically after you add more than 2, which is the style I was going for. Just to clear, for just buttons set title and message to nil as well.
Yes. It's called a UIAlertController
You can use an UIAlertController
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:#""
message:#"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *searchAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Search for an image", #"search action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
//Add your code
}];
UIAlertAction *choosePhotoAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Choose Photo", #"choosePhoto action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
//Your code
}];
UIAlertAction *takePhotoAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Take Photo", #"takePhoto action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
//Your code
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"cancel action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"cancel action");
}];
[alertController addAction:searchAction];
[alertController addAction:choosePhotoAction];
[alertController addAction:takePhotoAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
Yes, UIAlertController is provided for setting other controls on alert view.
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Username";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Password";
textField.secureTextEntry = YES;
}];
[self presentViewController:alert animated:YES completion:nil];
#Richa solution but in swift version
let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
let searchAction = UIAlertAction(title: NSLocalizedString("Search for an image", comment: "search action"), style: .default, handler: {(action: UIAlertAction) -> Void in
//Add your code
})
let choosePhotoAction = UIAlertAction(title: NSLocalizedString("Choose Photo", comment: "choosePhoto action"), style: .default, handler: {(action: UIAlertAction) -> Void in
//Your code
})
let takePhotoAction = UIAlertAction(title: NSLocalizedString("Take Photo", comment: "takePhoto action"), style: .default, handler: {(action: UIAlertAction) -> Void in
//Your code
})
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "cancel action"), style: .default, handler: {(action: UIAlertAction) -> Void in
print("cancel action")
})
alertController.addAction(searchAction)
alertController.addAction(choosePhotoAction)
alertController.addAction(takePhotoAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)