How to access the control of one viewcontroller into another viewcontroller?

PreranaPrerana INMember ✭✭

The scenario is as follows:-

I have my MainWindowController, it consists of a button.

I have another viewController say 'B' which consists of label.

when i click on button i want the label on B_ViewController to change.

In short i want to access a control of one viewcontroller in other viewcontroller and manipulated its value


I tried using outlets but those are accessible within its own viewcontroller and not other.

A quick search on google, i read about make one controller as other one's delegate and notify the delegate when the state of segment control changed

Can anyone tell me what exactly this mean and how to approach this?

Any suggestions or code snippets will be highly appreciated.

Thanks a ton in advance!!

Answers

  • SteveFlackSteveFlack GBMember ✭✭

    You could do it via an (objective-c) delegate-style system (create an interface with a method in it, one VC implements the interface, the other have a public property that's set to point to the first VC, it calls the method on the first VC when the button is clicked).

    Alternatively, another way to do it would have the VC with the button raise an event and have the second as a listener on the event (this is more akin to the objective-C NSNotificationCenter system).

  • PreranaPrerana INMember ✭✭

    @Steve,

    Can you please elaborate on first part of your answer?. It says i got to create a interface which contains a method in one VC. My 2nd VC will implement that interface,till here everything is crystal clear.

    the other have a public property that's set to point to the first VC



    Sorry I kind of got confused.

    It will be great and very helpful if you can explain it with a example.

    Thanks a ton for your answer..:)

  • PreranaPrerana INMember ✭✭

    I tried doing this way:-


    MainWindow controller:-

    1.) One button and customview

    2.)Loading the say firstview VC in mainwindow.

        this.cstview.AddSubview( new firstviewController().View);
    


    firstview VC:-

    1.)Declared a method say "changelabel()";

    2.)In change label I set the value of label as

       firstlabel.StringValue="changed"; 
    


    Now in MainWindowController i set the buttonclick listener to changelabel() of firstViewcontroller as follows:-

        firstviewcontroller a = new firstviewcontroller ();
        mainbtn.Activated+= (sender, e) => a.changelabel();
    

    The changelabel() actually gets called, the Console.writeln statements gets executed but it throws NullReference Exception when i try to change the label value.

  • PreranaPrerana INMember ✭✭

    Is it possible to send the whole instance of MainWindowController to other viewcontroller??

    Basically i want two ViewControllers to communicate with each other.

    Thanks a ton in advance!

Sign In or Register to comment.