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

Cdub16

macrumors member
Original poster
May 30, 2008
32
0
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.
 
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.
 
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
 
When you do this:

Code:
      if(firstViewController ==nil){
     firstViewController = [[FirstViewController alloc]init];
}

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.
 
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
 
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.
 
you will want to connect the IBoutlet to the button or the lable it just depends what action you want to occur
 
Code:
#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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.