PDA

View Full Version : IBAction/IBOutlet




Nnavick
Jan 29, 2011, 02:11 PM
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!



cnstoll
Jan 29, 2011, 03:28 PM
Set the text of the second label from the action method of the first button?

Nnavick
Jan 29, 2011, 03:36 PM
Set the text of the second label from the action method of the first button?

But how do I connect them?They are placed in separate views and classes

cnstoll
Jan 29, 2011, 04:32 PM
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.

Nnavick
Jan 29, 2011, 10:56 PM
What delegate should I use?

cnstoll
Jan 29, 2011, 11:31 PM
In the example I described, you would create your own protocol and use delegation to communicate with the other view controller.

If you don't already know what that means then it's probably beyond the scope of this thread to explain it, but hopefully this will help:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

xingworld
Jan 30, 2011, 05:24 AM
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:

View1Controller.h
--------------------------------------------------------------------------------
#import <UIKit/UIKit.h>
#import "YourAppDelegate.h"

@class YourAppDelegate;

@interface View1Controller : UIViewController
{
YourAppDelegate *m_poAppDelegate;
.......
}

View1Controller.m
--------------------------------------------------------------------------------
- (IBAction)buttonDoSomethingPressed:(id)sender
{
self.m_poAppDelegate = [[UIApplication sharedApplication] delegate];
// change view of View2Controller
[[[self.m_poAppDelegate m_poSwitchViewController] m_poView2Controller] somefunc:somevars];
}

Nnavick
Jan 30, 2011, 11:08 AM
.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 ?

cnstoll
Jan 30, 2011, 11:35 AM
In the header (interface) for TestAppDelegate, and implement it in the implementation file.

Nnavick
Jan 30, 2011, 11:46 AM
In the header (interface) for TestAppDelegate, and implement it in the implementation file.



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?
?

Nnavick
Jan 30, 2011, 12:27 PM
I did everything but what type the "m_poSwitchViewController" should be?

ViewController1 type or ViewController2 type?

ViewController1 *m_poSwitchViewController;
ViewController2 *m_poSwitchViewController;

?

cnstoll
Jan 30, 2011, 05:55 PM
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];

xingworld
Feb 1, 2011, 09:14 AM
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:
SwitchViewController *m_poSwitchViewController;
and make it as your root view.

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