UILabel changing text

Discussion in 'iOS Programming' started by Cdub16, Dec 24, 2009.

  1. Cdub16 macrumors member

    Joined:
    May 30, 2008
    #1
    ok. im getting pretty pissed off. this should be easy to do. but i am obviously missing something

    i have a two view controllers with two views. in the first view there is a label.
    and in the second view there is a text field and a button.
    when i press that button i want the value of the label in the first view to be set to the value of the text box in the second view.

    so i basically have two classes and i want an attribute of one class to be equal to an attribute of the other class.
    but the label isnt changing value.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How does the second view controller know about the label in the first view? Also, you might trying posting some code to help us better understand what you are trying to do.
     
  3. Cdub16 thread starter macrumors member

    Joined:
    May 30, 2008
    #3
    here you go

    Code:
    #import <UIKit/UIKit.h>
    
    @interface FirstViewController: UIViewController{
             IBOutlet UILabel *label;
    }
    @property(nonotomic, retain)IBOutlet UILabel *label;
    @end
    
    
    #import "FirstViewController.h"
    @implementation FirstViewController
    @synthesize label;
    
    @end
    
    ________________________________________________________________
    
    
    
    #import <UIKit/UIKit.h>
    #import "FirstViewController.h"
    @interface SecondViewController: UIViewController{
             IBOutlet UITextField *textField;
            FirstViewController *firstViewController;
            
    }
    -(IBAction)buttonPressed:(id)sender;
    @end
    
    #import "SecondViewController.h"
    @implementation SecondViewController
    
    -(IBAction)buttonPressed:(id)sender{
          if(firstViewController ==nil){
         firstViewController = [[FirstViewController alloc]init];
    }
        NSString *string = [textField text];
    
       [firstViewController.label setText:string];
    
    }
    @end
    
    
     
  4. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #4
    When you do this:

    You're creating a new instance of FirstViewController. Make firstViewController (the variable) an IBOutlet, connect it to the instance of FirstViewController that you've (I assume) got instanced in IB, and it should work.

    Basically, firstViewController shouldn't be nil. If it's nil, you don't have a link to the instance that already exists.
     
  5. Cdub16 thread starter macrumors member

    Joined:
    May 30, 2008
    #5
    ok i made it an IBOutlet, but im confused as to what i should be connecting it too.
    i cant connect it to the other view cuz thats not possible
     
  6. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #6
    Connect it to the instance of the view controller. Create a new object in interface builder and change its class to firstViewController, then plug it in the IBOutlet.
     
  7. hoosierfan24 macrumors member

    Joined:
    Jul 31, 2009
    #7
    you will want to connect the IBoutlet to the button or the lable it just depends what action you want to occur
     
  8. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #8
    I think you're making this more complicated than it needs to be. A simple way:

    Code:
    #import <UIKit/UIKit.h>
    @class FirstViewController;
    @interface SecondViewController: UIViewController{
             IBOutlet UITextField *textField;
            IBOutlet FirstViewController *firstViewController;
            
    }
    -(IBAction)buttonPressed:(id)sender;
    @end
    
    #import "SecondViewController.h"
    #import "FirstViewController.h"
    @implementation SecondViewController
    
    -(IBAction)buttonPressed:(id)sender{
          
        NSString *string = [textField text];
    
       [firstViewController.label setText:string];
    
    }
    @end
    With that code, all you need to do is connect the two views in interface builder. What you were doing was calling another instance of the FirstView instead of editing the one you want.
     

Share This Page