Alert not showing on the modal that is presenting - swift

On a view Controller when I click on the guard button -->
#IBAction func guardTapped(_ sender: Any) {
let vc = guardViewController.instanceFromNib()
vc.modalPresentationStyle = .overCurrentContext
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.present(vc, animated: true, completion: nil)
}
}
So In this guardViewController which is a presenting modal, I want to show an alert message when I click on the close button on guardViewController.
#IBAction func closeTapped(_ sender: Any) {
let alert = UIAlertController(title: "Alert", message: "Are You Sure", preferredStyle: .alert)
let proceedAction = UIAlertAction(title: "Proceed", style: .destructive) { (action) in
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.dismiss(animated: true)
}
}
alert.addAction(proceedAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
//
}
alert.addAction(cancelAction)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = sender as? UIView
popoverController.sourceRect = CGRect(x: (sender as AnyObject).bounds.midX, y: (sender as AnyObject).bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = [.any]
}
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.present(alert, animated: true, completion: nil)
}
}

Try presenting simple Alert with this Alert helper:
struct Alert {
static func showErrorAlertWithOk(on vc: UIViewController, withTitle: String, andMessage: String) {
let alert = UIAlertController(title: withTitle, message: andMessage, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel) {_ in }
alert.addAction(action)
vc.present(alert, animated: true)
}
}
Then use is as folows:
Alert.showErrorAlertWithOk(on: yourViewController, withTitle: "SampleAlert", andMessage: "YourMessage")

Related

I wanna control crop area in ImagePicker

After selecting the image from the gallery, I can crop it as a still frame to crop it. I don't want this. I want to be able to change the crop size. I want to be able to change the crop area like in the second gif. How can I do this without resorting to a 3rd party library ?
Image Picker Manager:
class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker = UIImagePickerController();
var alert = UIAlertController(title: "Fotoğraf Seç", message: nil, preferredStyle: .actionSheet)
var viewController: UIViewController?
var pickImageCallback : ((UIImage) -> ())?;
override init(){
super.init()
let cameraAction = UIAlertAction(title: "Kamerayı Aç", style: .default){
UIAlertAction in
self.openCamera()
}
let galleryAction = UIAlertAction(title: "Galeriden Seç", style: .default){
UIAlertAction in
self.openGallery()
}
let cancelAction = UIAlertAction(title: "İptal", style: .cancel){
UIAlertAction in
}
// Add the actions
picker.allowsEditing = true
picker.delegate = self
alert.addAction(cameraAction)
alert.addAction(galleryAction)
alert.addAction(cancelAction)
}
func pickImage(_ viewController: UIViewController, _ callback: #escaping ((UIImage) -> ())) {
pickImageCallback = callback;
self.viewController = viewController;
alert.popoverPresentationController?.sourceView = self.viewController!.view
viewController.present(alert, animated: true, completion: nil)
}
func openCamera(){
alert.dismiss(animated: true, completion: nil)
if(UIImagePickerController .isSourceTypeAvailable(.camera)){
picker.sourceType = .camera
self.viewController!.present(picker, animated: true, completion: nil)
} else {
let alertController: UIAlertController = {
let controller = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
controller.addAction(action)
return controller
}()
viewController?.present(alertController, animated: true)
}
}
func openGallery(){
alert.dismiss(animated: true, completion: nil)
picker.sourceType = .photoLibrary
self.viewController!.present(picker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage {
pickImageCallback?(image)
} else if let image = info[.originalImage] as? UIImage {
pickImageCallback?(image)
}
picker.dismiss(animated: true, completion: nil)
}
#objc func imagePickerController(_ picker: UIImagePickerController, pickedImage: UIImage?) {
}
}
My code result:
I want do this:

How to make actionsheet pop over a bar button on iPhone

I know how to make an action sheet pop over a button on iPad but not for iPhone. it seems that on iphone the action sheet doesn't have popoverPresentationController:
#IBAction func addChannel(_ sender: Any) {
let sender = sender as? UIBarButtonItem
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let addChanel = UIAlertAction(title: "Add a Channel", style: .default) { (_) in
self.addChannel()
}
let addContact = UIAlertAction(title: "Add a Contact", style: .default){ _ in
self.addContact()
}
actionSheet.addAction(addChanel)
actionSheet.addAction(addContact)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
// actionSheet.popoverPresentationControlle
if let popover = actionSheet.popoverPresentationController{
actionSheet.popoverPresentationController?.barButtonItem = sender
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
}
self.present(actionSheet, animated: true)
}
I want the action sheet pop over a button just like what WeChat + button does. Other stackoverflow answers are too old and not workable
Any idea is welcome!
Source.
You cannot use UIAlertController as a popover on iPhones. One of alternatives is using a UITableView or something inside new UIViewController, which you can create like described below (Swift 5.0).
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBOutlet private weak var button: UIButton!
#IBAction func buttonAction() {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.blue
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.modalPresentationStyle = .popover
let ppc = vc.popoverPresentationController
ppc?.permittedArrowDirections = .any
ppc?.delegate = self
ppc?.sourceView = button
present(vc, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Also, popoverPresentationController has property called barButtonItem which you can assign to your nav bar button.

How to set textfield placeholder's value to time picker's selected value

On "func can()" pop up shows a textfield on which we click & a time picker shows.I want to set textfield placeholder value to that selected picker's value
#IBAction func can(_ sender: UIButton) {
let alertController : UIAlertController = UIAlertController(title: "", message: "Confirm Order ?", preferredStyle: UIAlertController.Style.alert)
alertController.addTextField
{
(textField : UITextField!) -> Void in
textField.placeholder = "Expected Time"
textField.addTarget(self, action: #selector(self.myTargetFunction), for: .touchDown)
let okAction = UIAlertAction(title: "CANCEL", style: UIAlertAction.Style.destructive) {
(UIAlertAction) in
}
let CANAction = UIAlertAction(title: "SUBMIT", style: UIAlertAction.Style.default) {
(UIAlertAction) in
// self.actionCancelOrderApiCall()
}
alertController.addAction(okAction)
alertController.addAction(CANAction)
self.present(alertController, animated: true, completion: nil)
}
}
#objc func myTargetFunction(textField: UITextField) {
var datePicker : UIDatePicker = UIDatePicker(frame: CGRect(x: 0,y: 500, width: self.view.frame.size.width, height: 250))
datePicker.datePickerMode = .time
datePicker.backgroundColor = UIColor.white
if datePicker.isHidden == false{
tblV.isHidden = true
atchment1.isHidden = true
}
self.view.addSubview(datePicker)
}
The below changes should make it work
textField.addTarget(self, action: #selector(self.myTargetFunction(sender:)), for: .touchDown)
change your method signature to
#objc func myTargetFunction(sender: UITextField)
inside myTargetFunction(),
sender.placeholderText = datePicker.date

How do I add an image taken by camera or galery to a button?

I'm using an UIImagePickerController to choose a source to take a picture from. This picture should be set to an UIButton.
I know you can add an UIImage to an UIButton by string, like
someButton.setImage(UIImage(named:...)), but I need to set it by a variable which I create with taking the picture.
#IBOutlet weak var addPictureOutlet: UIButton!
func chooseSource() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let kameraAction = UIAlertAction(title: "Kamera",
style: .default) { (action) in
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}
let libraryAction = UIAlertAction(title: "Galerie",
style: .default) { (action) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
let alert = UIAlertController(title: "Quelle",
message: "Wählen sie eine Quelle für die Klausur.",
preferredStyle: .actionSheet)
alert.addAction(kameraAction)
alert.addAction(libraryAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
#IBAction func addPicture(_ sender: UIButton) {
chooseSource()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image1 = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
addPictureOutlet.setImage(image1, for: .normal)
}
}
I've tried this, but I can't really figure out why it doesn't work, there is no error while compiling or running and the UIImage of the UIButton does not change.
You should use like that. You cannot use a func in an action.
#IBOutlet weak var addPictureOutlet: UIButton!
func chooseSource() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let kameraAction = UIAlertAction(title: "Kamera",
style: .default) { (action) in
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}
let libraryAction = UIAlertAction(title: "Galerie",
style: .default) { (action) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
let alert = UIAlertController(title: "Quelle",
message: "Wählen sie eine Quelle für die Klausur.",
preferredStyle: .actionSheet)
alert.addAction(kameraAction)
alert.addAction(libraryAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image1 = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
addPictureOutlet.setImage(image1, for: .normal)
}
#IBAction func addPicture(_ sender: UIButton) {
chooseSource()
}
As you said it is not going into imagePicker method, it seems you have missed delegate to work imagePicker method properly
Add UIImagePickerControllerDelegate and create a variable to assign UIImagePickerController var imagePicker = UIImagePickerController()
and set imagePicker.delegate = self

Storyboard or segue from ViewController to Tabbar Controller

I want to implement a segue / storyboard in a button FROM View Controller TO tab bar controller (Home) in swift 3
My storyboard
This is my sign in code
#IBAction func LogIn(_ sender: Any) {
if self.emailTextfield.text == "" || self.passwordTextfield.text == "" {
let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
FIRAuth.auth()?.signIn(withEmail: self.emailTextfield.text!, password: self.passwordTextfield.text!) { (user, error) in
if error == nil {
print("You have successfully logged in")
//this is my storyboard but it gives me black screen
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
I tried using storyboard
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
i tried using segue
self.performSegue(withIdentifier: "Home", sender: self)
both gives me black screen. why? I have a clarification also. where should i connect my segue, is it in the tab bar controller(grey) or to the first TAB controller?
Try Below step,
1) Create new function in AppDelegate class
func LoginNav()
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainTab") as! tabbarControllerViewController // U have to create tabbarControllerViewController for ur TabBarView
self.window?.rootViewController = mainViewController
self.window?.makeKeyAndVisible()
}
2) Then call that func from any viewcontroller like below
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.LoginNav()