0

i have 2 classes . in my first class i have one label. now i have to give input for that label from my second class. here is my code.

 IBOutlet UILabel *label1;

@property(nonatomic, retain) IBOutlet UILabel *label1;

@synthesize label1;

I call this label like this. I import my class1 and create object like classone. I checked(NSLog print)class and that method will be called but that input won't come.i checked that label its also connected to my class.because i give same input in my viewDidLoad that time its working fine.

 NSString *ram= @":13123123312";
        classone.label1.text= ram;

guide me where i'm doing wrong.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Gurumoorthy Arumugam
  • 2,129
  • 1
  • 25
  • 40

4 Answers4

1

Setting values of previous views is trickier than this. What it the view has been removed by the OS under memory pressure?

The proper way of setting these values is to use the MVC pattern that is used throughout the Cocoa frameworks. Your second view controller sets a property of the previous view controller. And when the previous view needs to be shown, it takes its value from this property.

The usual way to correctly hook up a view controller to talk back to a another view controller lower in the stack is to use a delegate protocol.

I wrote an example of this, DelegationExample,a while ago which shows how a textfield in the first view is populated by a textfield's value in the second view controller using a delegate protocol. You might find it useful to see how I have done this as an example.

Update

I've updated the link to a new project for iOS6 with ARC and Storyboards

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

Take one NSString variable in AppDelegate class and synthesize it properly. And store the value of the second class's variable to that like:

appDelegate.strLbl = [NSString stringWithformat:@"%@",strVal];

and then copy that value to the label in first class like:

lblVal.text = [NSString stringWithformat:@"%@",appDelegate.strLbl];

Hope that helps you. Thanks.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Yama
  • 2,649
  • 3
  • 31
  • 63
  • Actually it isn't that good. It violates OOP, and using the AppDelegate as a general storage class is a beginner's mistake. – Abizern Dec 20 '11 at 12:51
  • Can you guide me how it violated OOP? – Yama Dec 20 '11 at 12:56
  • You're storing a value that is used in your first view in the AppDelegate. The only reason you are doing that is so that the second view controller can change it. So, the data you need for your first view controller isn't encapsulated in one class. – Abizern Dec 20 '11 at 12:59
  • Okay and does it affect the loading speed of the app and other memory issues? – Yama Dec 20 '11 at 13:01
  • You'll need to run it through a profiler to see how much of an affect it has - but that's not the problem here. The bad design is the problem. The way to do it is by using a delegate protocol. – Abizern Dec 20 '11 at 13:03
0

Actually you should have the reference of the first class in the second class. You should not allocate a new instance. If you create new instance, then the instance for which you have set the label value is different from the actual one which you will see on clicking back.

I guess you got this.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
-1

Aadhira was right, when u create a new instance of class1 in class2 its wrong, You have to get the original instance of class1, this can be achieved by creating a static function which returns current instance of class1, as shown below

static  classone*  sInstance;

@implementation classone

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    sInstance = self;
    return self;
}

+(classone*) getInstance {
   // NSAssert (sInstance!=nil, @"classone:getInstance: called when singleton was not initialized!");
    return sInstance;
}
Nakkeeran
  • 15,296
  • 1
  • 21
  • 22