Swift TimePicker Show Into UIAlerController with Cancel and Save Button Action - ios

My Scenario, I am trying to show TimePicker (Only showing HH:MM and AM or PM) within UIAlertController with Cancel and Save Button action. I tried below code but Its not working.
let myDatePicker: UIDatePicker = UIDatePicker()
myDatePicker.timeZone = NSTimeZone.local
myDatePicker.frame = CGRect(x: 0, y: 15, width: 270, height: 200)
let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.alert)
alertController.view.addSubview(myDatePicker)
let selectAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { _ in
print("Selected Date: \(myDatePicker.date)")
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
alertController.addAction(selectAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion:{})

fixed this issue by using below line
myDatePicker.datePickerMode = .time

Related

How to show selected action in UIAlertAction?

I am trying to achieve the view with a few green actions, and get the selected action to be bold in UIAlertController. And one of the actions, which is dismiss button has to be divided from others and be colored with red.
I am trying to add them with the styles .cancel allows to show dismiss button, but it is bold, but green.
How do I achieve this?
expected view:
my current code:
let alertController
= UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
let sortByDateAction = UIAlertAction(title: "По дате",
style: .default,
handler: {(action: UIAlertAction!) in
if self.sortBy != "date" {
self.page = 1
self.sortBy = "date"
self.loadTenders()
}
})
let moreExpensiveSortAction = UIAlertAction(title: "Дороже",
style: .destructive,
handler: {(action: UIAlertAction!) in
if self.sortBy != "priceHigh" {
self.page = 1
self.sortBy = "priceHigh"
self.loadTenders()
}
})
let cheaperSortAction = UIAlertAction(title: "Дешевле",
style: .default,
handler: {(action: UIAlertAction!) in
if self.sortBy != "priceLow" {
self.page = 1
self.sortBy = "priceLow"
self.loadTenders()
}
})
alertController
.addAction(sortByDateAction)
alertController
.addAction(moreExpensiveSortAction)
alertController
.addAction(cheaperSortAction)
alertController.preferredAction = sortByDateAction
alertController
.addAction(UIAlertAction(title: "Dismiss",
style: .cancel,
handler: nil))
let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:nil)
cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
alertController.addAction(cancelAlert)
try this code.
The color of the text in the action sheet is nothing but the tint color of the view. Set this as per your color requirement.
Furthermore, To change the font color of cancel button: Set the value for the titleTextColor property of the UIAlertAction
Here is a sample code:
func showCaptureOptions() {
let actionSheet = UIAlertController(title: "Capture Type", message: "", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { [weak self](action) in
self?.initializeImagePicker(captureType: .camera)
}
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { [weak self](action) in
self?.initializeImagePicker(captureType: .photoLibrary)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
}
cancelAction.setValue(UIColor.red, forKey: "titleTextColor"). /// The cancel button will be red
actionSheet.view.tintColor = UIColor.green /// The action sheet options would be green
actionSheet.addAction(cameraAction)
actionSheet.addAction(photoLibraryAction)
actionSheet.addAction(cancelAction)
DispatchQueue.main.async {
self.present(actionSheet, animated: true, completion: nil)
}
}

Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS

Please help! Im a huge beginner. Im trying to get a picker view and a textfield into one alert controller. Apparently I can't add a picker view into an alert controller since IOS 8. Instead I need to use an action sheet with multiple actions. Why can't I use a default alert style? Because I need more than 2 actions and the default alert style apparently only permits a max of 2 actions. So, I need to use an action sheet. Only problem is I can't seem to find a way to put an editable textfield into an action sheet, to have multiple action options - instead of an editable textfield in my default alert style, with only two action options.
This is what I have so far, for my editable textfield in my default alert style, with only two options (OK and cancel):
#IBOutlet weak var TEXTTESTLabel: UILabel!
#IBAction func TEXTESTTapped(_ sender: UIButton) {
print("TEXTTEST Button Tapped")
openTEXTTESTAlert()
}
func openTEXTTESTAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Test Your Text:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TEXTTESTLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "TEXTTEST"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
//////////////////////////////////////////////////////////////////////////////////
Since this, I've gotten help to create two separate alert controllers but it looks and feels messy. It's set up so if I click button 2 the default alert style with a textfield will pop and insert text into a label. But, if i click button 1, AND THEN I click button 2, it will show an action sheet with 4 actions that put a word into that same label. This is what I have for that method:
#IBOutlet weak var TextLabel: UILabel!
var userWantsToShowAlert = false
#IBAction func yourNewButtonTapped(_ sender: UIButton) {
userWantsToShowAlert = !userWantsToShowAlert
print("User wants to show alert? \(userWantsToShowAlert)")
//This is userWantsToShowAlert is false, it will change it to true. And if it is true, it will change it to false.
}
#IBAction func TextButtonTapped(_ sender: UIButton) {
print("Text Button Tapped")
if(userWantsToShowAlert){
openTextAlert()
}else{
openActionSheetAlert()
}
}
func openTextAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TextLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Whatever text you want to enter"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
func openActionSheetAlert(){
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
let bt1 = UIAlertAction(title: "1", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 1"}
alert9.addAction(bt1)
let bt2 = UIAlertAction(title: "2", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 2"}
alert9.addAction(bt2)
let bt3 = UIAlertAction(title: "3", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 3"}
alert9.addAction(bt3)
let bt4 = UIAlertAction(title: "4", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 4"}
alert9.addAction(bt4)
alert9.popoverPresentationController?.sourceView = self.view
alert9.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 4.0, width: 1.0, height: 1.0)
self.present(alert9, animated:true, completion: nil)
}
I find that this method is confusing to the application user. If anyone has a different method to get a textfield into an action sheet (or even a separate alert controller imbedded in a current alert controller), I'd appreciate it VERY much!
Thank you :)
For Action sheet you can use as
func showActionSheet()
{
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
actionSheet.addAction(UIAlertAction(title: "1:", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
)
self.action1()
}))
actionSheet.addAction(UIAlertAction(title: "2", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
self.action2()
}))
actionSheet.addAction(UIAlertAction(title: "3", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
// self.action3()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
// showActionSheet()
I found the solution! I was wrong about having only 2 actions in the default alert style. How to scroll through actions in alert controller? Xcode 8, Swift 3, IOS

Warning - Attempt to present UIAlertController null in Swift

I have an UIAlertController running in my project which is connected through UIButton.My code works ok.But,Everytime when I pressed the button Xcode console printing an alert as below and I went through all the similar question been asked in stackOverflow but no answer.Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy
2016-10-03 19:14:35.854 xxxxxx[85186:7214348] Warning: Attempt to present<UIAlertController: 0x7f8d99450820> on <yyyyyyy.ViewController: 0x7f8d9a025200> which is already presenting (null)
My Code Below:
override func viewDidLoad() {
super.viewDidLoad()
self.moreButton.frame = CGRect(x:view.frame.width / 2, y: view.frame.height / 2, width: 100, height: 40)
moreButton.center = view.center
moreButton.backgroundColor = UIColor.darkText.withAlphaComponent(0.4)
moreButton.layer.masksToBounds = true
moreButton.setTitle("More", for: UIControlState())
moreButton.setTitleColor(UIColor.white, for: UIControlState())
moreButton.showsTouchWhenHighlighted = true
moreButton.layer.cornerRadius = 5.0
moreButton.addTarget(self, action: #selector(ViewController.moreButtonAction(_:)), for: .allTouchEvents)
self.view.addSubview(self.moreButton)
}
func moreButtonAction(_ sender: UIButton) {
DispatchQueue.main.async(execute: {
let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8)
let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
})
let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
})
let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
})
let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {
(action: UIAlertAction!) in
})
myAlert.addAction(myAction_1)
myAlert.addAction(myAction_2)
myAlert.addAction(myAction_3)
myAlert.addAction(myAction_4)
self.present(myAlert, animated: true, completion: nil)
})
}
Thanks in Advance
The error is telling you that you are trying to present an alertController that is already active. You could check if your controller has an active alert otherwise show it. This will remove the warning. So basically move your self.present row with the following:
if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! {
self.present(myAlert, animated: true, completion: nil)
}
Replace your moreActionButton function with the following:
func moreButtonAction(_ sender: UIButton) {
let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8)
let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
})
let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
})
let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler: {
(action: UIAlertAction!) in
print("3")
})
let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {
(action: UIAlertAction!) in
})
myAlert.addAction(myAction_1)
myAlert.addAction(myAction_2)
myAlert.addAction(myAction_3)
myAlert.addAction(myAction_4)
if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! {
self.present(myAlert, animated: true, completion: nil)
}
}

Custom Action controller, Swift

Does anyone know how I can code up this custom alert controller (The top area especially) - currently found on the Apple music App. Maybe there is a known library that can do it.
I am aware this is how you an action controller is coded?
let alertController = UIAlertController(title: nil, message: "Top Message Here", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// Drop View Pop up
}
alertController.addAction(cancelAction)
let action1 = UIAlertAction(title: "Action Title", style: .Default) { (action) in
//Code to Execute
}
alertController.addAction(action1)
self.presentViewController(alertController, animated: true) {
// ...
}
Here is an example:
#IBAction func show() {
let actionSheet = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: .ActionSheet)
let view = UIView(frame: CGRect(x: 8.0, y: 8.0, width: actionSheet.view.bounds.size.width - 8.0 * 4.5, height: 120.0))
view.backgroundColor = UIColor.greenColor()
actionSheet.view.addSubview(view)
actionSheet.addAction(UIAlertAction(title: "Add to a Playlist", style: .Default, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Create Playlist", style: .Default, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Remove from this Playlist", style: .Default, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(actionSheet, animated: true, completion: nil)
}
If you're ok with deviating from the UIKit components, I have created a library for custom action sheets. It's written in Swift and lets you create custom sheets with several built-in item types. It can be extended and restyled as well.

How to create UIAlertView with Image

Hello everyone i have a question, if is possible to create UIAlertView with Image in Swift and how? If someone could help me because i don't find some example on network in swift practice all in objective c in iOS 6 not in iOS 7 or higher. Thanks.
After iOS8 UIAlertView is deprecated so Apple recommends you to use UIAlertController instead of UIAlertView. And to accomplish what you asked you can look here
Here is a workaround that I used successfully.
let myImage = UIImage(name:"myImage")
let prevImage = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 172))
prevImage.image = myImage
let spacer = "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
let previewController = UIAlertController(title: "PREVIEW", message: spacer, preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "SAVE", style: .Default) { (UIAlertAction) in
self.uploadPatientPhoto(myImage!)
}
let retakeAction = UIAlertAction(title: "RETAKE", style: .Default) { (UIAlertAction) in
self.retakeNewPatientPhoto(self)
}
let cancelAction = UIAlertAction(title: "CANCEL", style: .Cancel, handler: nil)
previewController.addAction(saveAction)
previewController.addAction(retakeAction)
previewController.addAction(cancelAction)
previewController.view.addSubview(prevImage)
presentViewController(previewController, animated: true, completion: nil)

Resources