I have an UIAlertController that allows a user to add a name and description of their smart advice. I would now like for the UIAlertController to allow the user to upload an image from their camera roll. I don't have any database or coredata enabled (Do I need to?). Here is my UIAlertController:
#IBAction func refreash(_ sender: AnyObject) {
let alert = UIAlertController(title: "Add Smart Device", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "Enter Name of the Smart Device Here"
})
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "Enter Desc of the Smart Device Here"
})
// I want something like alert.addImagePicker here
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
if let name = alert.textFields?.first?.text,
let desc = alert.textFields?[1].text {
self.mySmartDeviceList.addSmartDevice(name: name, desc: desc, image: "robot.jpg")
let indexPath = IndexPath (row: self.mySmartDeviceList.getCount() - 1, section: 0)
self.cityTable.beginUpdates()
self.cityTable.insertRows(at: [indexPath], with: .automatic)
self.cityTable.endUpdates()
}
}))
self.present(alert, animated: true)
}
See how it says that the image is just "robot.jpg"? I want that to be the image that the user uploaded.
If your app allow user use offline, u need to use local database to save it by id,name, And save file in local by id, then upload those picture when online.
I suggest do not save NSdata in CoreData.
Related
I'm trying to create an alert with two input fields which contain a master password for this app. It's my first app. I saw a function online and wanted to see if it still works but it doesn't seem to pop up with the alert when the function is called. Has this been changed in Swift 4?
func showInputDialog() {
//Creating UIAlertController and
//Setting title and message for the alert dialog
let alertController = UIAlertController(title: "Choose Master Password", message: "Enter your Master and confirm it!", preferredStyle: .alert)
//the confirm action taking the inputs
let confirmAction = UIAlertAction(title: "Enter", style: .default) { (_) in
//getting the input values from user
let master = alertController.textFields?[0].text
let confirm = alertController.textFields?[1].text
if master == confirm {
self.labelCorrect.isHidden = true
self.labelCorrect.text = master
}
}
//the cancel action doing nothing
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
//adding textfields to our dialog box
alertController.addTextField { (textField) in
textField.placeholder = "Enter Master"
}
alertController.addTextField { (textField) in
textField.placeholder = "Confirm Password"
}
//adding the action to dialogbox
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
//finally presenting the dialog box
self.present(alertController, animated: true, completion: nil)
}
//1. Create the alert controller.
let alert = UIAlertController(title: "Title", message: "Description", preferredStyle: .alert)
//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.placeholder = "User"
}
alert.addTextField { (textFieldPass) in
textFieldPass.placeholder = "Password"
textFieldPass.isSecureTextEntry = true
}
// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "Cancelar", style: .default, handler: { [weak alert] (_) in
print("cerrar")
}))
alert.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0]
let textFieldPass = alert?.textFields![1]
print("Text field: \(textField!.text)")
print("Text field: \(textFieldPass!.text)")
self.Autentificacion(usuario: textField!.text!, clave: textFieldPass!.text!)
}))
// 4. Present the alert.
self.present(alert, animated: true, completion: nil)
Going off as a reference to your question and daniel fernando muñoz melendez answer, I have written some code below.
//Create the alert controller.
let alert = UIAlertController(title: "Login", message: "For User", preferredStyle: .alert)
//Add the text field. You can configure it however you need.
alert.addTextField { (userField) in
userField.placeholder = "User"
}
alert.addTextField { (passWordField) in
passWordField.placeholder = "Password"
passWordField.isSecureTextEntry = true
}
//the cancel action doing nothing
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive)
//the confirm action taking the inputs
let acceptAction = UIAlertAction(title: "Enter", style: .default, handler: { [weak alert] (_) in
guard let userField = alert?.textFields?[0], let passWordField = alert?.textFields?[1] else {
print("Issue with Alert TextFields")
return
}
guard let userName = userField.text, let passWord = passWordField.text else {
print("Issue with TextFields Text")
return
}
print("Text field: \(userName)")
print("Text field: \(passWord)")
// Condition Logic
})
//adding the actions to alertController
alert.addAction(acceptAction)
alert.addAction(cancelAction)
// Presenting the alert
self.present(alert, animated: true, completion: nil)
The guard statement for the alert.textFields act as a check to make sure the text fields are in fact created and are not nil.
The guard statement for the userField.text is to make sure that the text in them can be cast to a String. It should be noted that even if the user does not enter anything into the text fields it will still run properly. Since you want them to enter in credentials, I would assume you don't want this to happen and recommend putting your condition logic there. Or have it like daniel fernando muñoz melendez did it and have it in a separate function.
Let me know if you need any clarification.
I want the user to press a button, and then for them to be able to see an alert where they can enter an input (to set a price for a service). The other logic involves saving data to a database, which is not really relevant to my problem.
I am using the following example:
https://stackoverflow.com/a/30139623/2411290
It definitely works, in that it shows the alert correctly, but once I include
print("Amount: \(self.tField.text)")
"self.tField.text" is not recognized. The specific error I get is:
value of type 'testVC' has no member 'tField'
#IBAction func setAmount(_ sender: Any) {
var tField: UITextField!
func configurationTextField(textField: UITextField!)
{
print("generating textField")
textField.placeholder = "Enter amount"
tField = textField
}
func handleCancel(alertView: UIAlertAction!)
{
print("Cancelled")
}
let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
print("Done !!")
}))
self.present(alert, animated: true, completion: {
print("completion block")
print("Amount: \(self.tField.text)") // Error here
})
//// other logic for app
}
tField is a local variable inside your setAmount function. It is not a property of the class.
Change:
self.tField.text
to:
tField.text
That will allow you to access the local variable.
But the real question is why are you creating a local variable of UITextField inside this function? Why are you printing its text when the text field isn't used anywhere?
Most likely you should be accessing the alert's text field inside the action handler for the "Done" button. There's no need to do anything inside the completion block of presenting the alert.
#IBAction func setAmount(_ sender: Any) {
let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField) in
print("generating textField")
textField.placeholder = "Enter amount"
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("Cancelled")
})
alert.addAction(UIAlertAction(title: "Done", style: .default) { (action) in
print("Done !!")
if let textField = alert.textFields?.first {
print("Amount: \(textField.text)")
}
})
self.present(alert, animated: true, completion: nil)
}
My guess is that when you present an alert your current ViewController is the alert viewController... And in your alert there is no variable tField.
On the exampled you quoted the alert was presented only after the print with tField's value. That why that worked there and doesn't work in your case.
This is how I simply create UIAlertController and present it on the screen:
private class func showAlertWithTitle(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
//alert.accessibilityLabel = "my string here" //doesnt work
let action = UIAlertAction(title: "OK", style: .Default) { action in
alert.dismissViewControllerAnimated(true, completion: nil)
}
alert.addAction(action)
UIStoryboard.topViewController()?.presentViewController(alert, animated: true, completion: nil)
}
and this is how I access it under UITests:
emailAlert = app.alerts["First Name"] //for title "First Name"
but I would like to set there custom identifier and access this by firstName like this:
emailAlert = app.alerts["firstName"]
Is it possible?
This is an old thread but someone might use this.
I was able to set the accessibility identifier like this:
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.view.accessibilityIdentifier = "custom_alert"
alert.view.accessibilityValue = "\(title)-\(message)"
alert.addAction(
UIAlertAction(
title: "ALERT_BUTTON_OK".localized,
style: .default,
handler: handler
)
)
present(alert, animated: true)
That way I can access the alert by accessibility identifier and check its contents in accessibility value.
It is not perfect of course, but it works - at least for my testing using Appium.
The only way I figured out to do this was to use Apple's private APIs. You call valueForKey on the UIAlertAction object with this super secret key: "__representer" to get whats called a _UIAlertControllerActionView.
let alertView = UIAlertController(title: "This is Alert!", message: "This is a message!", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertView.addAction(okAction)
self.presentViewController(alertView, animated: true, completion: {
let alertButton = action.valueForKey("__representer")
let view = alertButton as? UIView
view?.accessibilityIdentifier = "okAction_AID"
})
This has to be done in the completion handler because that that _UIAlertControllerActionView won't exist until the view is presented. On a side note in my project I used these following extensions to make things easier / more readable:
extension UIAlertController {
func applyAccessibilityIdentifiers()
{
for action in actions
{
let label = action.valueForKey("__representer")
let view = label as? UIView
view?.accessibilityIdentifier = action.getAcAccessibilityIdentifier()
}
}
}
extension UIAlertAction
{
private struct AssociatedKeys {
static var AccessabilityIdentifier = "nsh_AccesabilityIdentifier"
}
func setAccessibilityIdentifier(accessabilityIdentifier: String)
{
objc_setAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier, accessabilityIdentifier, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
func getAcAccessibilityIdentifier() -> String?
{
return objc_getAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier) as? String
}
}
So the above code would be rewritten:
let alertView = UIAlertController(title: NSLocalizedString("NMN_LOGINPAGECONTROLLER_ERROR_TITLE", comment: ""), message: message as String, preferredStyle:.Alert)
let okAction = UIAlertAction(title: NSLocalizedString("NMN_OK", comment: ""), style: .Default, handler: nil)
okAction.setAccessibilityIdentifier(InvalidLoginAlertView_AID)
alertView.addAction(okAction)
self.presentViewController(alertView, animated: true, completion: {
alertView.applyAccessibilityIdentifiers()
})
My first attempt involved trying to navigate the view hierarchy but that became difficult since UIAlertControllerActionView was not a part of the public API. Anyway I'd probably would try to ifdef out the valueForKey("__representer") for builds submitted for the app store or Apple might give you a spanking.
Right now I have a UIAlertAction called addCamera and I'm just doing:
addCamera.accessibilityLabel = "camera-autocomplete-action-photo"
That allows me to tap it in UI Tests as follows:
app.sheets.buttons["camera-autocomplete-action-photo"].firstMatch.tap()
From Apple docs...
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html
Making Alert Views Accessible
Alert views are accessible by default.
Accessibility for alert views pertains to the alert title, alert message, and button titles. If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.”
I am working on an application that saves user's created images to the photo library. I need to attach a user name to each saved image so that I can recall these later. As far as I understand (please correct me if I am wrong), if I am saving to the photo library, I can't change the actual file name, so a way around this is to change modify the metadata and write the user name to the description for instance. However, I am completely stuck on how to accomplish this in Swift and I have searched everywhere for an answer. The code I have to save each UIImage is as follows: I have the save option and the input for a user name presented in an alert view.
// Create the alert controller
let alertController = UIAlertController(title: "Finished?", message: "Is this your attempt?", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "Yes, save", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
if let image = self.mainImageView.image{
UIImageWriteToSavedPhotosAlbum(image,self,Selector("image:withPotentialError:contextInfo:"),nil)
}
}
let cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
self.mainImageView.image = nil
}
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter User Name"
self.inputTextField = textField
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
And I have another function for the image, which is as follows:
func image(image: UIImage, withPotentialError error: NSErrorPointer, contextInfo: UnsafePointer<()>) {
NSLog("\(self.inputTextField.text)")
let alertController = UIAlertController(title: "Success!", message:"Your image was saved", preferredStyle: .Alert)
//Some additional code here, specific to another part of the app that has to occur after the image is saved
self.presentViewController(alertController, animated: true){}
}
This code works to save an image, however I am completely at a loss for how to add anything to the metadata of an image. If anyone has any suggestions or advice it would be greatly appreciated and please let me know if anything is unclear and I can edit or clarify in the comments.
I'm new to swift and now following a tutorial book. The book is kind outdated, and I had this bug.
Basically I'm trying to do a table cell selection, after I selected the cell it should pop up a menu and then I can hit the call button. However, right now after I hit the call button, the expected alter box doesn't show up, and the compiler gives me the error: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
There is no bug when I edit the code, it just doesn't run correctly.
Another problem is that I have about 17 rows on the table, the stimulator's screen only shows 7 and I can't scroll down to see the rest of them.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath){
//this creates an option menu when you tap on the row
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .ActionSheet)
//this is what happened after you hit the cancel option
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
//defines the action after tap on the call option
let nocallAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
let callActionHandler = {(action:UIAlertAction!)-> Void in
let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
alertMessage.addAction(nocallAction)
}
//this is what happened after you hit the call option
let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style:
UIAlertActionStyle.Default, handler: callActionHandler)
//this is what happened after you hit the been there option
let isVisitedAction = UIAlertAction(title: "I've been here", style: .Default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
})
//add the action to the option menu
optionMenu.addAction(isVisitedAction)
optionMenu.addAction(callAction)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}
You never present the view controller in the callActionHandler - it should look like this:
let callActionHandler = {(action:UIAlertAction!)-> Void in
let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
alertMessage.addAction(nocallAction)
self.presentViewController(alertMessage, animated: true, completion: nil)
}
Swift also allows you to create the completion handler at the same time as the UIAlertAction, which I think is more readable:
let callAction = UIAlertAction(title: "Call " + "123-000-243534", style: UIAlertActionStyle.Default)
{ action in
let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
alertMessage.addAction(nocallAction)
self.presentViewController(alertMessage, animated: true, completion: nil)
}