RaviG RaviG - 13 days ago 5
Swift Question

Update label text when click on popover in swift

i have popover in may form

i have created separate viewController(TableViewController) for popover

everything is fine up to this

but when i click on cell of popover the value of label Will change to popovers cell text but its not happening

i tried using protocol but its not working

so please help

my Popover Class is

import UIKit

class GenderPopoverController: UITableViewController {

var gen = ["Male", "Female"]
var selectionHandler : ((selectedItem: String) -> Void!)?
var selectedItem: String!
var x = UITextField()
var delegate: DismissGenderPopoverDelegate?

override func viewDidLoad() {
super.viewDidLoad()

//preferredContentSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: 100)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.gen.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.gen[indexPath.row]
return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectionHandler?(selectedItem: self.gen[indexPath.row])
selectedItem = self.gen[indexPath.row]
delegate?.didSelectGender(selectedItem)
dismissViewControllerAnimated(true, completion: nil)

}
}

protocol DismissGenderPopoverDelegate{
func didSelectGender(SelectedGender:String)
}


an my View Controller In which text field is present is

import UIKit

class createProfilePage: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIPopoverControllerDelegate, DismissGenderPopoverDelegate, UIPopoverPresentationControllerDelegate{

@IBOutlet weak var myTableView: UITableView!
var name_TF: UITextField!
var email_TF: UITextField!
var sixdigitpin_TF: UITextField!
var DOB_TF: UITextField!
var gender_TF: UITextField!
var marritalStatus_TF: UITextField!
var Add1_TF: UITextField!
var Add2_TF: UITextField!
var state_TF: UITextField!
var city_TF: UITextField!
var pin_TF: UITextField!
var industry_TF: UITextField!
var hIncome_TF: UITextField!
var currentTextField: UITextField!
var geb: String!
var popover: UIViewController? = nil
static let sharedInstance = createProfilePage()

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification , object: nil)
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if(indexPath.row == 0){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! CreateProfileCell1
cells.profileImage.image = UIImage(named: "photo.jpg")
// cells.profileImage.layer.cornerRadius = cells.profileImage.frame.size.width/2
// cells.profileImage.clipsToBounds = true
cell = cells
}else if(indexPath.row == 1){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! CreateProfileCell2
cells.name_textField.delegate = self
self.name_TF = cells.name_textField
cell = cells
}else if(indexPath.row == 2){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell3", forIndexPath: indexPath) as! CreateProfileCell3
cells.email_textField.delegate = self
self.email_TF = cells.email_textField
cell = cells
}else if(indexPath.row == 3){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell4", forIndexPath: indexPath) as! CreateProfileCell4
cells.sixDigitPin_textField.delegate = self
self.sixdigitpin_TF = cells.sixDigitPin_textField
cell = cells
}else if(indexPath.row == 4){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell5", forIndexPath: indexPath) as! CreateProfileCell5
cells.DateOfBirth_textField.delegate = self
self.DOB_TF = cells.DateOfBirth_textField
cell = cells
}else if(indexPath.row == 5){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell6", forIndexPath: indexPath) as! CreateProfileCell6
cells.gender_textField.delegate = self
let tap = UITapGestureRecognizer(target: self, action: "Tapped:")
cells.gender_textField.addGestureRecognizer(tap)

self.gender_TF = cells.gender_textField
cell = cells
}else if(indexPath.row == 6){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell7", forIndexPath: indexPath) as! CreateProfileCell7
cells.maritalStatus_textField.delegate = self
self.marritalStatus_TF = cells.maritalStatus_textField
cell = cells
}else if(indexPath.row == 7){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell8", forIndexPath: indexPath) as! CreateProfileCell8
cells.addressLine1_textField.delegate = self
self.Add1_TF = cells.addressLine1_textField
cell = cells
}else if(indexPath.row == 8){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell9", forIndexPath: indexPath) as! CreateProfileCell9
cells.addressLine2_textField.delegate = self
self.Add2_TF = cells.addressLine2_textField
cell = cells
}else if(indexPath.row == 9){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell10", forIndexPath: indexPath) as! CreateProfileCell10
cells.state_textField.delegate = self
self.state_TF = cells.state_textField
cell = cells
}else if(indexPath.row == 10){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell11", forIndexPath: indexPath) as! CreateProfileCell11
cells.city_textField.delegate = self
self.city_TF = cells.city_textField
cell = cells
}else if(indexPath.row == 11){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell12", forIndexPath: indexPath) as! CreateProfileCell12
cells.pincode_textField.delegate = self
self.pin_TF = cells.pincode_textField
cell = cells
}else if(indexPath.row == 12){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell13", forIndexPath: indexPath) as! CreateProfileCell13
cells.industry_textField.delegate = self
self.industry_TF = cells.industry_textField
cell = cells
}else if(indexPath.row == 13){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell14", forIndexPath: indexPath) as! CreateProfileCell14
cells.houseoldIncome_textField.delegate = self
self.hIncome_TF = cells.houseoldIncome_textField
cell = cells
}else if(indexPath.row == 14){
let cells = self.myTableView.dequeueReusableCellWithIdentifier("Cell15", forIndexPath: indexPath) as! CreateProfileCell15
cells.save_button.addTarget(self, action: "ButtonAction:", forControlEvents: .TouchUpInside)
cell = cells
}

return cell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.row == 0){
return 200
}else if(indexPath.row == 14){
return 60
}else{
return 50
}
}



func Tapped(gestureRecognizer: UITapGestureRecognizer){

//let tableView:GenderPopoverController = GenderPopoverController(style: UITableViewStyle.Plain)
//tableView.delegate = self
popover = self.storyboard?.instantiateViewControllerWithIdentifier("genderPop") as! GenderPopoverController
popover!.modalPresentationStyle = .Popover
popover!.preferredContentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, 100)

let popoverPresentationViewController = popover!.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = UIPopoverArrowDirection.Any
popoverPresentationViewController?.delegate = self
popoverPresentationViewController?.sourceView = gender_TF
popoverPresentationViewController?.sourceRect = CGRectMake(gender_TF.frame.width, gender_TF.frame.height*3, 0, 0)
presentViewController(popover!, animated: true, completion: nil)
}

func didSelectGender(SelectedGender: String) {
print("DSG")
self.gender_TF.text = SelectedGender
self.popover!.dismissViewControllerAnimated(true, completion: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "ShowGender"){
let vc = segue.destinationViewController as UIViewController
let controller = vc.popoverPresentationController
}
}

func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
print("Dismisss")
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}

func ButtonAction(button: UIButton){

let interestPage = self.storyboard?.instantiateViewControllerWithIdentifier("selectInterest")as! SelectInterestPage
//self.navigationController?.pushViewController(PhotosPage, animated: true)
self.presentViewController(interestPage, animated: true, completion: nil)
}

func keyboardWillShow(sender: NSNotification){
let frame = (sender.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
myTableView.setContentOffset(CGPointMake(0, frame.height-100), animated: true)
}

func textFieldShouldReturn(textField: UITextField) -> Bool{

textField.resignFirstResponder()
return true
}

func keyboardWillHide(sender: NSNotification){
myTableView.setContentOffset(CGPointMake(0, 0), animated: true)
}

func textFieldDidBeginEditing(textField: UITextField) {
if(textField == DOB_TF){
let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))
let datePickerView : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 0, 0))
datePickerView.datePickerMode = UIDatePickerMode.Date
inputView.addSubview(datePickerView)

let doneButton = UIButton(frame: CGRectMake((self.view.frame.size.width/2) - (100/2), 0, 100, 50))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.setTitle("Done", forState: UIControlState.Highlighted)
doneButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
doneButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
inputView.addSubview(doneButton)

doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside)
textField.inputView = inputView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

handleDatePicker(datePickerView)

}else if(textField == email_TF){
print("Email")
}

currentTextField = textField
}

func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
self.DOB_TF.text = dateFormatter.stringFromDate(sender.date)
}

func doneButton(sender:UIButton)
{
self.view.endEditing(true)
}

func textFieldDidEndEditing(textField: UITextField) {
currentTextField = nil
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Answer

I did it using Protocol Create Protocol and call a protocol method Where you want data from Popover