I need an option like image picker for picking contact and to display phone number i have managed to get contact names using below code
by using this code it only returns the names , need an option to pick contact from contact list
import ContactsUI
and include - CNContactPickerDelegate
import ContactsUI
class YourViewController: CNContactPickerDelegate{
//MARK:- contact picker
func onClickPickContact(){
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactGivenNameKey
, CNContactPhoneNumbersKey]
self.present(contactPicker, animated: true, completion: nil)
}
func contactPicker(_ picker: CNContactPickerViewController,
didSelect contactProperty: CNContactProperty) {
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
// You can fetch selected name and number in the following way
// user name
let userName:String = contact.givenName
// user phone number
let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value
// user phone number string
let primaryPhoneNumberStr:String = firstPhoneNumber.stringValue
print(primaryPhoneNumberStr)
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
}
}
import ContactsUI
private let contactPicker = CNContactPickerViewController()
Button click that initiates contact picker:
#IBAction func accessContacts(_ sender: Any) {
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)
}
Implement delegate methods
extension YourViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let phoneNumberCount = contact.phoneNumbers.count
guard phoneNumberCount > 0 else {
dismiss(animated: true)
//show pop up: "Selected contact does not have a number"
return
}
if phoneNumberCount == 1 {
setNumberFromContact(contactNumber: contact.phoneNumbers[0].value.stringValue)
} else {
let alertController = UIAlertController(title: "Select one of the numbers", message: nil, preferredStyle: .alert)
for i in 0...phoneNumberCount-1 {
let phoneAction = UIAlertAction(title: contact.phoneNumbers[i].value.stringValue, style: .default, handler: {
alert -> Void in
self.setNumberFromContact(contactNumber: contact.phoneNumbers[i].value.stringValue)
})
alertController.addAction(phoneAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
dismiss(animated: true)
self.present(alertController, animated: true, completion: nil)
}
}
func setNumberFromContact(contactNumber: String) {
//UPDATE YOUR NUMBER SELECTION LOGIC AND PERFORM ACTION WITH THE SELECTED NUMBER
var contactNumber = contactNumber.replacingOccurrences(of: "-", with: "")
contactNumber = contactNumber.replacingOccurrences(of: "(", with: "")
contactNumber = contactNumber.replacingOccurrences(of: ")", with: "")
contactNumber = contactNumber.removeWhitespacesInBetween()
guard contactNumber.count >= 10 else {
dismiss(animated: true) {
self.popUpMessageError(value: 10, message: "Selected contact does not have a valid number")
}
return
}
textFieldNumber.text = String(contactNumber.suffix(10))
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
}
}
You can use this extension to get a contact name.
extension CNContact {
open func displayName() -> String {
return givenName + " " + familyName
}
}
This is Class to get Some details EPContact
Swift 5 & Contact Picker Get Email
I wanted to show an example of how you can do the same thing you asked for but for email instead and updated for Swift 5 since some of the answers do not compile correctly above. This also has the added bonus of the 'all' option which will concatenate multiple emails into one string if the user selects it or not.
First make sure to import import ContactsUI
Then make sure you have an outlet to your textfield.
#IBOutlet var emailTextField: UITextField!
Next you need to set the contact picker as a member variable of your viewController. This will hold the information for showing the contact picker later.
class EmailViewController: UIViewController {
#IBOutlet var emailTextField: UITextField!
private let contactPicker = CNContactPickerViewController()
//... rest of view controller code, etc...
}
Finally you can simply add this extension to the EmailViewController with the code below:
extension EmailViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let emailNumberCount = contact.emailAddresses.count
//#JA - They have to have at least 1 email address
guard emailNumberCount > 0 else {
dismiss(animated: true)
//show pop up: "Selected contact does not have a number"
let alertController = UIAlertController(title: "No emails found for contact: "+contact.givenName+" "+contact.familyName, message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Ok", style: .default, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
return
}
//#JA - If they have only 1 email it's easy. If there is many emails we want to concatenate them and separate by commas , , ...
if emailNumberCount == 1 {
setEmailFromContact(contactEmail: contact.emailAddresses[0].value as String)
} else {
let alertController = UIAlertController(title: "Select an email from contact: "+contact.givenName+" "+contact.familyName+" or select 'All' to send to every email listed.", message: nil, preferredStyle: .alert)
for i in 0...emailNumberCount-1 {
let emailAction = UIAlertAction(title: contact.emailAddresses[i].value as String, style: .default, handler: {
alert -> Void in
self.setEmailFromContact(contactEmail: contact.emailAddresses[i].value as String)
})
alertController.addAction(emailAction)
}
let allAction = UIAlertAction(title: "All", style: .destructive, handler: {
alert -> Void in
var emailConcat = ""
for i in 0...emailNumberCount-1{
if(i != emailNumberCount-1){ //#JA - Only add the , if we are not on the last item of the array
emailConcat = emailConcat + (contact.emailAddresses[i].value as String)+","
}else{
emailConcat = emailConcat + (contact.emailAddresses[i].value as String)
}
}
self.setEmailFromContact(contactEmail: emailConcat)//#JA - Sends the concatenated version of the emails separated by commas
})
alertController.addAction(allAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
dismiss(animated: true)
self.present(alertController, animated: true, completion: nil)
}
}
func setEmailFromContact(contactEmail: String){
emailTextField.text = contactEmail
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("contact picker canceled")
}
}
To call up the picker in the action event of a button for example you could do this:
#IBAction func contactsButtonPressed(_ sender: UIButton) {
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)
}
contactPicker.delegate = self works because of the extension on the viewController class (emailViewController) in my case that gives it access to the CNContactPickerDelegate protocol functions it requires.
If contact have more than one phone numbers, then you can get the selected number by comparing the 'contactProperty.identifier' as below -
public func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
tvName.text = contactProperty.contact.givenName
var selectedNo = ""
if contactProperty.key == "phoneNumbers" {
contactProperty.contact.phoneNumbers.forEach({ phone in
if phone.identifier == contactProperty.identifier {
selectedNo = phone.value.stringValue
}
})
}
tvContact.text = selectedNo
}
Related
I have tried multiple times to create a UITextField that when clicked should show the contacts available on the device and retrieve the phone number and display it in the textfield. However I have been unable to do that. The best that I could do is to use a button to receive and display the number on a textfield. This works! How do I do the same for when the UITextField is clicked?
I'm running it on Xcode 10
private let contactPicker = CNContactPickerViewController()
override func viewDidLoad() {
super.viewDidLoad()
configureTextFields()
configureTapGesture()
phonenumber.textContentType = .telephoneNumber
}
private func configureTextFields() {
phonenumber.delegate = self
}
private func configureTapGesture(){
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap))
viewcontact.addGestureRecognizer(tapGesture)
}
#objc private func handleTap(){
viewcontact.endEditing(true)
}
#IBAction func pbbbbb(_ sender: Any) {
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)
}
}
extension SelfTestTimer: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let phoneNumberCount = contact.phoneNumbers.count
guard phoneNumberCount > 0 else {
dismiss(animated: true)
return
}
if phoneNumberCount == 1 {
setNumberFromContact(contactNumber: contact.phoneNumbers[0].value.stringValue)
}else{
let alertController = UIAlertController(title: "Select one of the numbers", message: nil, preferredStyle: .alert)
for i in 0...phoneNumberCount-1 {
let phoneAction = UIAlertAction(title: contact.phoneNumbers[i].value.stringValue, style: .default, handler: {
alert -> Void in
self.setNumberFromContact(contactNumber: contact.phoneNumbers[i].value.stringValue)
})
alertController.addAction(phoneAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
dismiss(animated: true)
self.present(alertController, animated: true, completion: nil)
}
}
func setNumberFromContact(contactNumber: String) {
var contactNumber = contactNumber.replacingOccurrences(of: "-", with: "")
contactNumber = contactNumber.replacingOccurrences(of: "(", with: "")
contactNumber = contactNumber.replacingOccurrences(of: ")", with: "")
guard contactNumber.count >= 10 else {
dismiss(animated: true) {
self.presentAlert(alertTitle: "", alertMessage: "A maximum of 10 contacts allowed per session", lastAction: nil)
}
return
}
phonenumber.text = String(contactNumber.suffix(10))
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
}
}
extension SelfTestTimer: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
I want it so that when the UITextField is clicked, the contacts will appear and when one contact is selected, the number should appear in the textfield
You should use textFieldShouldBeginEditing method. Open the contacts controller in this method and return false, no need to add a gesture recogniser.
I want to bind my ImageView to viewModel for save my selected image to Core Data.
My code look like this:
class FoodViewModel: FoodViewModelType {
var foodImage: BehaviorRelay<UIImage?>
//... another code
}
My controller:
class NewFoodViewController: UIViewController {
#IBOutlet weak var foodImageView: UIImageView!
override func viewDidLoad() {
//... another code
self.foodImageView.rx.image.bind(to: foodViewModel.foodImage).disposed(by: self.disposeBag)
}
}
And i get error:
Value of type Binder < UIImage? > has no member 'bind'
How to save my image to Core Data with good MVVM practice?
Update
I am choose photo in view controller:
func chooseImagePickerAction(source: UIImagePickerController.SourceType) {
if UIImagePickerController.isSourceTypeAvailable(source) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = source
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
}
#objc func foodImageViewTapped(_ sender: AnyObject) {
let alertController = UIAlertController(title: "Photo path", message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.chooseImagePickerAction(source: .camera)
}
let photoLibAction = UIAlertAction(title: "Photo", style: .default) { (action) in
self.chooseImagePickerAction(source: .photoLibrary)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
alertController.addAction(cameraAction)
alertController.addAction(photoLibAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
extension NewFoodViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
foodImageView.image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage
foodImageView.contentMode = .scaleAspectFill
dismiss(animated: true, completion: nil)
}
private func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
}
private func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String {
return input.rawValue
}
}
And in viewDidLoad (without image):
saveNewFoodBarButtonItem.rx.tap.subscribe(onNext: { [weak self] _ in
guard let self = self else { return }
let foodViewModel = FoodViewModel()
self.foodQuantityTypeTextField.rx.text.bind(to: foodViewModel.foodQuantityType).disposed(by: self.disposeBag)
self.foodShelfLifeTextField.rx.text.bind(to: foodViewModel.foodShelfLife).disposed(by: self.disposeBag)
self.foodCategoryTextField.rx.text.bind(to: foodViewModel.foodCategoryId).disposed(by: self.disposeBag)
self.foodQuantityTextField.rx.text.bind(to: foodViewModel.foodQuantity).disposed(by: self.disposeBag)
self.foodNameTextField.rx.text.bind(to: foodViewModel.foodName).disposed(by: self.disposeBag)
foodViewModel.saveNewFood(fridgeViewModel: self.fridgeViewModel!)
self.dismiss(animated: true)
}).disposed(by: disposeBag)
UIImageView is not bindable because it is an output view, not an input view, i.e., you push things into it, it doesn't push things out.
In order to emit an image to your view model, you need to do it the at the point where you push the image into the UIImageView.
I came across a scenario where i had to call a function inside enum's function in swift 3.
The scenario is as follows:
class SomeViewController: UIViewController {
enum Address {
case primary
case secondary
func getAddress() {
let closure = { (text: String) in
showAlert(for: "")
}
}
}
func showAlert(for text: String) {
let alertController = UIAlertController(title: text, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title:NSLocalizedString("OK", comment:"OK button title"), style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
As you can see from the above code I get an error on line 10 (showAlert(for: ""))
The error is:
instance member showAlert cannot be used on type SomeViewController; did you mean to use a value of this type instead?
How can I call a function from enum's function then?
Alternative approach:
You can use a static method of SomeViewController to present the alert.
Example:
static func showAlert(for text: String)
{
let alertController = UIAlertController(title: text, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title:NSLocalizedString("OK", comment:"OK button title"), style: .cancel, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
}
Using it:
enum Address
{
case primary
case secondary
func getAddress()
{
let closure = { (text: String) in
SomeViewController.showAlert(for: "")
}
closure("hello")
}
}
override func viewDidLoad()
{
super.viewDidLoad()
let addr = Address.primary
addr.getAddress()
}
The enum does not know the instance and thus can not access its members. One approach to deal with this situation would be to inform the client of the enum that something went wrong.
class SomeViewController: UIViewController {
enum Address {
case primary
case secondary
func getAddress() -> String? {
//allGood == whatever your logic is to consider a valid address
if allGood {
return "theAddress"
} else {
return nil;
}
}
}
func funcThatUsesAddress() {
let address = Address.primary
guard let addressString = address.getAddress() else {
showAlert(for: "")
return
}
// use your valid addressString here
print(addressString)
}
func showAlert(for text: String) {
let alertController = UIAlertController(title: text, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title:NSLocalizedString("OK", comment:"OK button title"), style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
First of all please do not mark duplicate on this question Ive done my research on this topic, and not only have the most recent answers been from over a year ago, but they have also been in C#. Mine also differs from those because I am trying to present my UIView from what I assume to be a child of a child view. But I'm not 100% sure about this. So, here is what my code dump looks like after the suggestions.
import UIKit
import Firebase
class LoginViewController: UIViewController, UITextFieldDelegate{
#IBOutlet weak var usernameTxt: UITextField!
#IBOutlet weak var emailTxt: UITextField!
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var confirmPassTxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func nextScreen(sender: UIButton) {
if(emailTxt.text == "" || passwordTxt.text == "" || confirmPassTxt.text == "" || usernameTxt.text == ""){
let alertController = UIAlertController(title: "Wait!", message: "You didn't fill out the required fields, please do so and try again. ", preferredStyle: .Alert)
let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}else{
if(validateEmail(emailTxt.text!)){
emailCheck(emailTxt.text!){isValid in if isValid{self.secondRound({ (goodMail, goodPassL, goodPassTxt, nameGood) in
if (goodMail && goodPassL && goodPassTxt && !nameGood){
print("good")
}else{
self.showAlert("Else", description: "Got it?") }
})}else{let alertController=UIAlertController(title: "Whoops!", message: "That email address has already been taken, please try another one", preferredStyle: .Alert)
let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
alertController.parentViewController
self.presentViewController(alertController, animated: true, completion: nil)
}}
}else{
let alertController = UIAlertController(title: "Whoops!", message: "That doesnt appear to be a valid email address, please check your information and try again!", preferredStyle: .Alert)
let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
alertController.parentViewController
presentViewController(alertController, animated: true, completion: nil)
}
}
}
func showAlert(title: String, description: String){
let alertController: UIAlertController = UIAlertController(title: title, message: description, preferredStyle: .Alert)
let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
self.presentViewController(alertController, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestinationVC : Login2VC = segue.destinationViewController as! Login2VC
DestinationVC.prepareEmail = emailTxt.text!
DestinationVC.preparePass = passwordTxt.text!
}
func validateEmail(canidate: String) -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
return NSPredicate(format: "SELF MATCHES %#", emailRegex).evaluateWithObject(canidate)
}
func nameFilter(input : String)-> Bool{
var profanity : Bool = true
let dataRef = FIRDatabase.database().reference()
dataRef.child("Profanity").observeSingleEventOfType(.Value) { (snap: FIRDataSnapshot) in
if(snap.exists()){
if(snap.value! as! NSArray).containsObject(input){
print("our ears!")
profanity = true
}else{
profanity = false
}
}
}
return profanity
}
func emailCheck(input: String, callback: (isValid: Bool) -> Void) {
FIRAuth.auth()?.signInWithEmail(input, password: " ") { (user, error) in
var canRegister = false
if error != nil {
if (error?.code == 17009) {
canRegister = false
} else if(error?.code == 17011) {
//email doesn't exist
canRegister = true
}
}
callback(isValid: canRegister)
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func secondRound(callback:(goodMail:Bool, goodPassL:Bool, goodPassTxt:Bool, nameGood:Bool)->Void){
let availableEmail : Bool = true
var passwordMatch : Bool = false
var passwordLength : Bool = false
var profanity : Bool = false
if(passwordTxt.text!==confirmPassTxt.text!){passwordMatch=true}else{passwordMatch=false}
if(passwordTxt.text!.characters.count>=6&&confirmPassTxt.text!.characters.count>=6){passwordLength=true}else{passwordLength=false}
if(nameFilter(usernameTxt.text!)){profanity=true}else{profanity=false}
callback(goodMail: availableEmail, goodPassL: passwordLength, goodPassTxt: passwordMatch, nameGood: profanity)
}
}
Essentially, what I am trying to do is:
Check to see if the inputted text is formatted as an email correctly
Check to see if the name is available
Check if the username contains profanity (pulled as a json from firebase)
Check to see if the passwords match
Check too see if the passwords are at least 6 characters in length
Each false result would have its own UIAlertView that results from it, but whenever I try to add these views they do not show up, and the app returns with this error.
Note, the false event does intact appear in the first condition only. Other than that, nothing happens.
This is the error I have been getting, and while it seems pretty straight forward I cannot figure out hoe to fix it, nor find any recent information online on how to accomplish this task.
> Warning: Attempt to present <UIAlertController: 0x7fb513fb8bc0> on
> <myApp.LoginViewController: 0x7fb513c6e0a0> whose view is not in the
> window hierarchy!
The logical answer to this would be:
Hey, why don't you just add the window to the hierarchy and be done?
Well, to that question I have a question, if the window is not apart of the hierarchy, then why is the view itself even displaying. Furthermore, why would the first set of UIAlerts be displaying, but as soon as I get into a nested if they cease? Any ideas on how to fix this terrible error?
Thanks all
Here is my code
import Foundation
import UIKit
class Alert: UIViewController {
class func showAlert(title: String, message: String, buttonTitle: String? = nil, buttonTitle2: String? = nil, sender: UIViewController) -> Int? {
var flag : Int?
func yes(){
flag = 1
}
func no(){
flag = 0
}
//= nil ;
if objc_getClass("UIAlertController") != nil {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
if (buttonTitle != nil){
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
yes()
}))
}
if (buttonTitle2 != nil) {
alert.addAction(UIAlertAction(title: buttonTitle2, style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
no()
}))
}
sender.presentViewController(alert, animated: true, completion: nil)
return flag
} else {
let alert = UIAlertView()
alert.title = title
alert.message = message
alert.addButtonWithTitle(buttonTitle)
alert.addButtonWithTitle(buttonTitle2)
print("inside Else")
alert.show()
return flag
//make and use a UIAlertView
}
}
}
The problem is that the flag getting returned is always nil to the view controller I call Alert from. I need the flag to return 1 if "yes" is pressed and 0 if "No" is pressed. I have been stuck in this problem for lot of time. Help.
Try this code,
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Hey there", message: "Do you want to do it?", preferredStyle: .Alert)
let btnAction1 = UIAlertAction(title: "Yes", style: .Default) { (action : UIAlertAction) -> Void in
self.yes()
}
let btnAction2 = UIAlertAction(title: "No", style: .Default) { (action : UIAlertAction) -> Void in
self.no()
}
alertController.addAction(btnAction1)
alertController.addAction(btnAction2)
presentViewController(alertController, animated: true, completion: nil)
}
func yes() -> Int
{
print("Yes")
return 1
}
func no() -> Int
{
print("No")
return 0
}
}
Its working fine here, i hope it will work for u also.. :)
You made a mistake that closure won't execute until button clicked but function will return right now.You should deliver a closure to this function.It will look like:
class func showAlert(title: String, message: String, buttonTitle: String? = nil, buttonTitle2: String? = nil, sender: UIViewController,handler: ((Bool) -> Void)) -> Void
And handle it in alert action like this:
handler(true)