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

Access the result of variable from one class in to another class

Discussion in 'iPhone/iPad Programming' started by iphonejudy, Apr 9, 2009.

  1. macrumors 6502

    iphonejudy

    #1
    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:mad:"Newsmenu" bundle:[NSBundle mainBundle]];

    ......
    .....

    }

    - (IBAction) previousbutton:(id)sender
    {
    button =@"previous"
    RootviewController *vController = [[RootviewController alloc]
    initWithNibName:mad:"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:
    HTML:
    result=rootviewcontroller.button 
    1)What is the way to access the "button" value in RootviewController class.
     
  2. macrumors 6502a

    #2
    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:

    Code:
    vController.someVar = someValue
    
     
  3. macrumors 6502

    iphonejudy

    #3
    v can assign value to a variable easily.

    but i want the value of the variable
     
  4. macrumors 6502a

    #4
    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:

    Code:
    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.
     
  5. macrumors 6502

    iphonejudy

    #5
    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:mad:"Newsmenu" bundle:[NSBundle mainBundle]];

    ......
    .....

    }

    - (IBAction) previousbutton:(id)sender
    {
    button =@"previous"
    RootviewController *vController = [[RootviewController alloc]
    initWithNibName:mad:"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:
    HTML:
    result=rootviewcontroller.button 
    1)What is the way to access the "button" value in RootviewController class.
     
  6. macrumors regular

    #6
    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.
     
  7. macrumors 6502a

    #7
    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:

    Code:
    //within your nextButton method:
    
    vController.button = @"next";
    
    
     

Share This Page