Lydia
Lydia

Reputation: 2117

Reload MainViewController when PopViewController dismissed

I have a main view controller and pop up controller.Please refer screen shots.

Main view controller

POPUP view controller

Code :

             @IBAction func show(sender: AnyObject) {

    var popView = popupviewcontroller(nibName:"popview",bundle:nil)
    var popController = UIPopoverController(contentViewController: popView)

    popController.popoverContentSize = CGSize(width: 450, height: 450)
  popController.presentPopoverFromRect(sender.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Down, animated: true)
}

For the popupViewcontoller i used .xib.

When press save button data saved to core data.

Lets come to my problem, in my mainViewController i fetched data and fill them in dynamically created lables.That occurred when view load.I want to reload mainViewController when close button form popViewController pressed.

I tried within the close button my code are here, i just tried to reload the mainVc :

         var mainVC = mainviewcontroller()         
    @IBAction func close(sender: AnyObject) {
 self.dismissViewControllerAnimated(true, completion: nil)
       //mainVc.viewDidLoad()
     mainVC.reloadInputViews()
   }

Does not give output. Conclusion : I want a way to refresh view controller from another view in swift.

Thanks in advance.

Upvotes: 1

Views: 8653

Answers (5)

dscrown
dscrown

Reputation: 578

Swift 2, try

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

Works for me

Upvotes: 0

Lydia
Lydia

Reputation: 2117

With God Grace i found a solution that is , just create a Global variable

             Var fetchedArray = Nsarray()

Then Write the following code in the popovoer controller save button

     func save(){
          // Write codes for saving data 

           let request = NSFetchRequest(entityName: "Enitity name")

        request.returnsObjectsAsFaults = false

        let   Total   = try? context.executeFetchRequest(request)



        if let wrapResults = Total {

            fetchedArray = wrapResults

        }


             }

Then fill the Labels by using fetchedArray.

Thanks

Upvotes: 0

Shanmugasundharam
Shanmugasundharam

Reputation: 2092

Using UITableView only we can reload the data!,So we have to use table view custom cell textfield or Label. Now we can able to reload our custom textfield data.

Upvotes: 2

Roman Barzyczak
Roman Barzyczak

Reputation: 3813

You should use protocol for it. You can read more about protocols here https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

You can make protocol in PopViewController

protocol PopViewControllerProtocol { static var valuesChanged() }

Later you should implement this protocol in MainViewController and refresh data.

Example:

File UIPopoverController.swift

protocol UIPopoverControllerDelegate{
    func valuesChanged(changedValue:String)
}

class UIPopoverController: UIViewController {

var delegate: UIPopoverControllerDelegate! = nil
@IBAction func close(sender: AnyObject) {
  self.dismissViewControllerAnimated(true, completion: nil)
   //mainVc.viewDidLoad()
 mainVC.valuesChanged("some value")
}}

File MainViewController.swift

class MainViewController: UIViewController, UIPopoverControllerDelegate       
{
   func valuesChanged(changedValue:String) {
     //this will be called when popuviewcontroller call valueschanged on delegate object
   }

     @IBAction func show(sender: AnyObject) {

var popView = popupviewcontroller(nibName:"popview",bundle:nil)
var popController = UIPopoverController(contentViewController: popView)
popController.delegate = self;
popController.popoverContentSize = CGSize(width: 450, height: 450)
popController.presentPopoverFromRect(sender.frame, inView: self.view,      permittedArrowDirections: UIPopoverArrowDirection.Down, animated: true)
 }
}

Upvotes: 0

Eendje
Eendje

Reputation: 8883

Does your main ViewController use a UITableView?

You can use viewWillAppear and get the data again and use tableView.reloadData() to reload the data.

EDIT:

with var mainVC = mainviewcontroller() you're just making a new instance of your MainViewController. If you want to use reloadInputViews(), you can put it in viewWillLoad.

Upvotes: 0

Related Questions