PDA

View Full Version : Access the result of variable from one class in to another class




iphonejudy
Apr 9, 2009, 08:02 AM
Hi,


I need to display feeds based on the button,

I have two buttons called next and previous.


if i click next button , i need to display some feeds.

if i click previous button , i need to display some feeds.



I wrote the code in the following way


In main class , i wrote the following code



- (IBAction) nextbutton:(id)sender
{

button= @"next";
RootviewController *vController = [[RootviewController alloc]
initWithNibName:@"Newsmenu" bundle:[NSBundle mainBundle]];

......
.....

}

- (IBAction) previousbutton:(id)sender
{
button =@"previous"
RootviewController *vController = [[RootviewController alloc]
initWithNibName:@"Newsmenu" bundle:[NSBundle mainBundle]];

......
.....

}

..........end............


In RootviewController i wrote the main operations,so i need to check the variable "button"

if i call the "button" in the RootviewController class it displays null value.

code:
result=rootviewcontroller.button

1)What is the way to access the "button" value in RootviewController class.



eddietr
Apr 9, 2009, 11:06 AM
When you say "main class", what do you mean by that?

Why not create a property in the RootViewController and set that variable like this:


vController.someVar = someValue

iphonejudy
Apr 11, 2009, 12:13 AM
v can assign value to a variable easily.

but i want the value of the variable

eddietr
Apr 11, 2009, 09:49 AM
v can assign value to a variable easily.

but i want the value of the variable

I'm not sure if you understood what I said. So your problem is that from your vController you did not have a reference to the rootController.

So you can avoid that problem by setting a property on the vController itself. Then from within that view controller you can read it with:


self.someVar


Or there are other ways to do this, such as putting a reference to your rootController in the new view controller. Or putting this variable in your appDelegate. Or any number of other options. This is just one way to do it.

iphonejudy
Apr 13, 2009, 07:09 AM
Hi,


I need to display feeds based on the button,

I have two buttons called next and previous.


if i click next button , i need to display some feeds.

if i click previous button , i need to display some feeds.



I wrote the code in the following way


In Menuscreen class , i wrote the following code



- (IBAction) nextbutton:(id)sender
{

button= @"next";
RootviewController *vController = [[RootviewController alloc]
initWithNibName:@"Newsmenu" bundle:[NSBundle mainBundle]];

......
.....

}

- (IBAction) previousbutton:(id)sender
{
button =@"previous"
RootviewController *vController = [[RootviewController alloc]
initWithNibName:@"Newsmenu" bundle:[NSBundle mainBundle]];

......
.....

}

..........end............


In RootviewController i wrote the main operations,so i need to check the variable "button"

if i call the "button" in the RootviewController class it displays null value.

code:
result=rootviewcontroller.button

1)What is the way to access the "button" value in RootviewController class.

BlackWolf
Apr 13, 2009, 08:38 AM
you already posted that?

there are several ways to do this. the easiest way is to make an IBOutlet with the class "RootViewController" in your Menuscreen class, and then connect that to your root view controller (which probably is your file's owner in IB). this way, you can access everything in your root view controller.

eddietr
Apr 13, 2009, 11:19 AM
you already posted that?

there are several ways to do this. the easiest way is to make an IBOutlet with the class "RootViewController" in your Menuscreen class, and then connect that to your root view controller (which probably is your file's owner in IB). this way, you can access everything in your root view controller.

OK, this is where I got a bit confused earlier. In her case, the RootviewController is not the first view. The Menuscreen seems to be the first view, then it programatically creates a RootviewController and (wants to) set a property on that new instance. So I don't think IB will help here.

But iPhoneJudy, here is what you can do: Just create a property called "button" (or a better name) on RootviewController. Then when you create a new RootviewController, just set the property like so:


//within your nextButton method:

vController.button = @"next";