Swift 5 code - Control other tab in tabbar?

hi, could help me anybody? i need control other controller (mapView) in other UIViewController, i would like add to map annotation, but i cant control mapView controller from other class.. for example here is easy pic what i need:

item 1 (has button)
item 2 (has label)

i need click on button in item 1 .. and on item 2 change text in label

here is my project, thanks for help everybody
download ZIP

Hello,

Those forums are dedicated to the Swift language, not to Apple platforms, so your question is a little bit off-topic here. You are encouraged to ask this kind of question on Apple Developper forums, or StackOverflow.

Now, you are here, and it's possible to give a suggestion.

Ideally, both view controllers should not know about each other, so that they can live in isolation, and be tested. This means that the communication between the two has to be delegated to a third object.

Delegation is a standard iOS technique, that you already know about, according to your screenshot:

protocol LeftControllerDelegate: AnyObject {
    func leftControllerDidHitButton(_ controller: LeftController)
}

class LeftController: UIViewController {
    weak var delegate: LeftControllerDelegate?

    @IBAction func didHitButton(_ sender: UIButton) {
        delegate?.leftControllerDidHitButton(self)
    }
}

Which object should be the delegate? In your case, it's easy: it is the object that creates both view controllers and puts them in the UITabBarController. It has all the required information.

If your app creates the UITabBarController programmatically, from some class, then this class is the perfect delegate:

func makeTabBarController() -> UITabBarController {
    let left = /* LeftViewController */
    self.right = /* RightViewController */
    left.delegate = self // <-- there
    let tabBarController = UITabBarController()
    tabBarController.viewControllers = [left, right]
    return tabBarController
}

func leftControllerDidHitButton(_ controller: LeftController) {
    self.right.functionFromMain("Hello")
}

If your app lets the storyboard magic create the UITabBarController, then I'd suggest creating a subclass of UITabBarController, say MainTabBarController, update the storyboard and change the class of the tab bar controller to MainTabBarController, and make this tab bar controller the delegate of its child:

class MainTabBarController: UITabBarController: LeftControllerDelegate {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let left = viewControllers.first as? LeftViewController {
            left.delegate = self // <--
        }
    }

    func leftControllerDidHitButton(_ controller: LeftController) {
        if let right = viewControllers.last as? RightViewController {
            right.functionFromMain("Hello again")
        }
    }
}

sorry for wrong forum, i think that it is same, becouse it is in swift (5.1)

i created protocol and use in parent

protocol MyProtocol:class
{
    func requiredMethod(txt: String)
}

class ParentView: UITabBarController, MyProtocol {

    override func viewDidLoad() {
        super.viewDidLoad()
        requiredMethod(txt: "TEST1")
    }

    func requiredMethod(txt: String) {
        print(txt)
    }
}

TEST1 is working, but...

in UIViewController use protocol and call function

class start_ViewController: UIViewController {
    weak var deleg:MyProtocol?

    @IBAction func btn_addPoint(_ sender: UIButton) {
        deleg?.requiredMethod(txt: "TEST2")
    }
}

but not working, where is problem?

It's hard to say, because your last answer looks completely unrelated to your original question as well as the answer that was given to you.

I try to easy way for understand my problem...

i need control any object (UI) from other class, yes solution is with parent, but i dont know how must be set for working... i show easy way, becouse i need this solution for harder project ;) but for understanding it is enough