2

there was a similar problem: There is an object addinAprel in the class ViewController, also there is a class SecondController, it is necessary to transfer this object from one class to another in Swift.

Example programs:

Function in the ViewController class:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("Selected item is", current_arr[row])
active_textField.text = current_arr[row]
let realm = try! Realm()
let events = realm.objects(EventsDB.self)

if ((text_field_months.text) == "Апрель")
{
    if ((text_field_days.text) == "8")
    {
    let addinAprel=print(events.filter("dataMonth == %@ && dataDay ==%@",months_arr[3], days_arr[7]))
        print("very good")
        return addinAprel
    }
    if ((text_field_days.text) == "1")
    {
    let addinAprel=print(events.filter("dataMonth == %@ && dataDay ==%@",months_arr[3], days_arr[0]))
        print("very good")
        return addinAprel
    }
    print("good")
}
else{
    print("very bad")
}
}

Class SecondController:

import Foundation
import UIKit
import RealmSwift

class SecondController: UIViewController, UITableViewDelegate {
let realm = try! Realm()

override func viewDidLoad() {
super.viewDidLoad()

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You select \(massiv[indexPath.row])")

}
}
AustinZ
  • 1,787
  • 1
  • 13
  • 21
aaallleeexxx918
  • 171
  • 3
  • 11
  • https://stackoverflow.com/questions/47247885/in-app-purchase-call-function-when-sucessful/47247937#47247937 refer to this – carbonr Nov 14 '17 at 18:28
  • Your question appears to be related to a previous one. Now beware: StackOverflow isn't a "forum style" system; people will not follow you from one question to another. If your original question has not been resolved but you have make progress toward it, please edit the original question, for example by appending description of your progress directly inside the question. If this really is a distinct question, then please make the question more clear. You give a reasonable code sample, that's good, but have a look at the *Verifiable* subtitle on https://stackoverflow.com/help/mcve. – James Nov 14 '17 at 18:29

1 Answers1

1

It depends on the flow. There are multiple options like:

  1. Delegate pattern
  2. Notifications
  3. Passing via segue/unwind segue
  4. Passing via shared entity or service (Singleton for example)
  5. Referencing a ViewController directly
  6. Using storage (UserDefaults, databases etc.)

To answer the question you should provide more context.

From what I see you can use Option 5: Instantiate SecondController from ViewController (may keep it as a reference in class scope), set the property you want and present/push it. It's the most simple option.

I advise you to read about ViewControllers, Protocols/Delegation, Notifications more.

Oleh Zayats
  • 2,423
  • 1
  • 16
  • 26