Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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!
 
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.
 
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];
}
 
Last edited by a moderator:
.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 ?
 
In the header (interface) for TestAppDelegate, and implement it in the implementation file.
 
In the header (interface) for TestAppDelegate, and implement it in the implementation file.



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?
?
 
I did everything but what type the "m_poSwitchViewController" should be?

ViewController1 type or ViewController2 type?

ViewController1 *m_poSwitchViewController;
ViewController2 *m_poSwitchViewController;

?
 
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];
 
I did everything but what type the "m_poSwitchViewController" should be?

ViewController1 type or ViewController2 type?

ViewController1 *m_poSwitchViewController;
ViewController2 *m_poSwitchViewController;

?

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;
}
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.