Search code examples
iosuitableviewswift2popover

Change value in UITextField from PopOver UITableViewCell selection in Swift


I have an iOS Xcode7 Swift2 project I'm working on. I have an AddViewController that has a UITextField with a UIButton next to is. When it is pressed it brings a new PopOverViewController.

The code to initiate the PopOver is:

    let bounds = UIScreen.mainScreen().bounds
    let center = bounds.width/2

    var popoverContent = (self.storyboard?.instantiateViewControllerWithIdentifier("PopOverViewController"))! as UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)
    nav.modalPresentationStyle = UIModalPresentationStyle.Popover
    var popover = nav.popoverPresentationController
    popoverContent.preferredContentSize = CGSizeMake(200,115)
    popover!.delegate = self
    popover!.sourceView = self.view
    popover!.sourceRect = CGRectMake(center,140,0,0)

    self.presentViewController(nav, animated: true, completion: nil)

Then I see the PopOverViewControlleras shown in this photo:

enter image description here

The new PopOverViewController has a UITableView in it. I want it so when the user selects a folder in the UITableView, it changes the name from 'General' in this case to the selected 'Misc' in the original AddViewController. How do I do this?

I have tried in the PopOverViewController didSelectRowAtIndexPath:

let selected = selectableFolders[indexPath.row]
selectedFolder = selected

let priorViewController: AddViewController()
priorViewController.folderName.text = selectedFolder

This doesn't work however. Can someone please help me? Thank you!


Solution

  • let selected = selectableFolders[indexPath.row]
    selectedFolder = selected
    
    
    
       [ let priorViewController: AddViewController() ] 
    
        priorViewController.folderName.text = selectedFolder
    

    What i think is that this code does work all there is wrong with it is getting the priorViewController . You should not create another instance in the enclosed line (let priorViewController:AddViewController() instead get the view from the app delegate this is how i did it in one of my projects let messagesVC = (((UIApplication.sharedApplication().keyWindow?.rootViewController)as? LoginViewController)?.presentedViewController)?.childViewControllers[1] as? Messages

       messagesVC?.inputToolbar.contentView.textView.text = selected joke!
    

    This is all in my didselectrow . plus the code for getting the cell similar to yours. your view hierarchy might be different , can you show me that hierarchy or just try the above stated way first . Note: first try updating the view as you are on same page maybe it's not getting redrawn but i think the first code i gave will solve the problem .