1. Welcome to the new MacRumors forums. See our announcement and read our FAQ


Discussion in 'iPhone/iPad Programming' started by Nnavick, Jan 29, 2011.

  1. macrumors regular

    I have 2 views and a label in each of them,what I want to do is when I will press a button the text of the label in second view will be the same on the first,
    How do I connect them?

  2. macrumors 6502

    Set the text of the second label from the action method of the first button?
  3. macrumors regular

    But how do I connect them?They are placed in separate views and classes
  4. macrumors 6502

    Depends on how you have everything set up. You could create a delegate protocol to handle it. Create a protocol in ViewController1 that when a button is pressed it sends a message to it's delegate saying that the button was pressed and perhaps what to change the label to. Where you instantiate both view controllers, set the ViewController2 as the delegate of ViewController1. In ViewController2...implement that delegate method that tells it that a button was pressed in ViewController1.

    Note: this is all done in code. I know of no way to do this with Interface Builder.
  5. macrumors regular

    What delegate should I use?
  6. macrumors 6502

  7. xingworld, Jan 30, 2011
    Last edited by a moderator: Feb 1, 2011

    macrumors newbie

    1, You must switch the two views in a root view controller, I named it SwitchViewController.

    2, The two view are named View1Controller, View2Controller. so you will var the two in root view controller as member vars, for example:
    View1Controller	*m_poView1Controller;
    View2Controller	*m_poView2Controller;
    3, In View1Controller you connect a button, you can control the view of View2Controller by these code:

    #import <UIKit/UIKit.h>
    #import "YourAppDelegate.h"
    @class YourAppDelegate;
    @interface View1Controller : UIViewController
    	YourAppDelegate	*m_poAppDelegate;
    - (IBAction)buttonDoSomethingPressed:(id)sender
            self.m_poAppDelegate	= [[UIApplication sharedApplication] delegate];
    	//    change view of View2Controller
            [[[self.m_poAppDelegate m_poSwitchViewController] m_poView2Controller] somefunc:somevars];
  8. macrumors regular

    .h File
    WelcomePage *welcomePage;//The second view controller
    TestAppDelegate *m_poAppDelegate;
    .m file
    [[[self.m_poAppDelegate m_poSwitchViewController] welcomePage]change_Text];
    It's says

    TestAppDelegate' may not respond to '-m_poSwitchViewController'

    where should I declare m_poSwitchViewController ?
  9. macrumors 6502

    In the header (interface) for TestAppDelegate, and implement it in the implementation file.
  10. macrumors regular


    just to be clear

    this in the TestAppDelegate (.h file)
    WelcomePage *welcomePage;//The second view controller
    TestAppDelegate *m_poAppDelegate;
    and this in the ViewController1 .m

    [[[self.m_poAppDelegate m_poSwitchViewController] welcomePage]change_Text];

    And where and how I declare the m_poSwitchViewController?
  11. macrumors regular

    I did everything but what type the "m_poSwitchViewController" should be?

    ViewController1 type or ViewController2 type?

    ViewController1 *m_poSwitchViewController;
    ViewController2 *m_poSwitchViewController;

  12. macrumors 6502

    If you have welcomePage declared and synthesized as a property of your app delegate, then you should be able to just do this, without the switchViewController:

    [[self.m_poAppDelegate welcomePage] change_Text];
  13. xingworld, Feb 1, 2011
    Last edited by a moderator: Feb 1, 2011

    macrumors newbie

    m_poSwitchViewController is a view controller.
    you should var it in YourAppDelegate.h:
    SwitchViewController *m_poSwitchViewController;
    and make it as your root view.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    	// Override point for customization after application launch.
    	[window addSubview:m_poSwitchViewController.view];
    	[self.window makeKeyAndVisible];
    	//	create the queue to run our SubmitAnswerOperation
    	self.m_poQueue = [[NSOperationQueue alloc] init];
    	return YES;

Share This Page