IBAction/IBOutlet

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

  1. macrumors regular

    Joined:
    Oct 13, 2010
    #1
    Hi,
    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?

    Thanks!
     
  2. macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    Set the text of the second label from the action method of the first button?
     
  3. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #3
    But how do I connect them?They are placed in separate views and classes
     
  4. macrumors 6502

    Joined:
    Aug 29, 2010
    #4
    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. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #5
    What delegate should I use?
     
  6. macrumors 6502

    Joined:
    Aug 29, 2010
    #6
  7. xingworld, Jan 30, 2011
    Last edited by a moderator: Feb 1, 2011

    macrumors newbie

    Joined:
    Jan 24, 2011
    #7
    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:
    Code:
    View1Controller	*m_poView1Controller;
    View2Controller	*m_poView2Controller;
    
    3, In View1Controller you connect a button, you can control the view of View2Controller by these code:

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

    Joined:
    Oct 13, 2010
    #8
    .h File
    Code:
    WelcomePage *welcomePage;//The second view controller
    TestAppDelegate *m_poAppDelegate;
    
    .m file
    Code:
    [[[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

    Joined:
    Aug 29, 2010
    #9
    In the header (interface) for TestAppDelegate, and implement it in the implementation file.
     
  10. thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #10


    just to be clear

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

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

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

    Joined:
    Oct 13, 2010
    #11
    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

    Joined:
    Aug 29, 2010
    #12
    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:


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

    macrumors newbie

    Joined:
    Jan 24, 2011
    #13
    m_poSwitchViewController is a view controller.
    you should var it in YourAppDelegate.h:
    Code:
    SwitchViewController *m_poSwitchViewController;
    
    and make it as your root view.

    YourAppDelegate.m:
    Code:
    - (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