5

I'm new to development and been banging my head against a wall trying to figure this out, I'm sure, that I'm missing something silly but after trying various different solutions, I'm still unable to get the result I'm looking for.

I would like to be able to update a UILabel in a ViewController from another class, here is a little demo program that I cannot get to work, I have the ViewController which has two UILabels, one is updated from with the viewDidDoad and the other one I would like to update from the other class called NewClass which is called from ViewController, I can the see the class is being called correctly as the console is logging the NSLog entry but I cannot get the syntax for updating the UILabel.

Thanks in advance.

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController {

   UILabel *_labelupdate01;
   UILabel *_labelupdate02;     
 }

 @property (nonatomic, retain) IBOutlet UILabel *labelupdate01;
 @property (nonatomic, retain) IBOutlet UILabel *labelupdate02;

 @end

ViewController.m

#import "ViewController.h"
#import "NewClass.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize labelupdate01;
@synthesize labelupdate02;


- (void)viewDidLoad
{
    [super viewDidLoad];

    labelupdate01.text = @"Update from ViewController";

    [NewClass updatedisplay];
}
@end

NewClass.h

#import <Foundation/Foundation.h>

@class ViewController;

@interface NewClass : NSObject

@property (nonatomic, assign) ViewController *ViewController;

+ (void)updatedisplay;

@end

NewClass.m

 #import "NewClass.h"
 #import "ViewController.h"

 @implementation NewClass

 @synthesize ViewController = _ViewController;

 + (void)updatedisplay
 {
     NSLog(@"NewClass - updatedisplay");
     ViewController *labelupdate02;
     labelupdate02.labelupdate02.text = @"Update from NewClass";
 }

 @end
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
Wez
  • 57
  • 1
  • 8

3 Answers3

4

define this in newClass like that

+ (void)updatedisplay:(ViewController *)vc
{
     vc.labelupdate02.text=@"Update from NewClass";

}

and call it from your viewController

[NewClass updatedisplay:self];
meth
  • 1,887
  • 2
  • 18
  • 33
1

First, make sure you initialize ViewController in NewClass, then you can do whatever you want with it.

#import <Foundation/Foundation.h>

@class ViewController;

@interface NewClass : NSObject

@property (nonatomic, assign) ViewController *aViewController;

+ (void)updatedisplay;

@end

In implementation

 #import "NewClass.h"
 #import "ViewController.h"

 @implementation NewClass

 + (void)updatedisplay
 {

 _aViewController.labelupdate02.text = @"Update from NewClass";

 }

 @end
David
  • 14,205
  • 20
  • 97
  • 144
  • This method didnt work for me, I am getting an "Unknown type name aViewController" error, I have updated the @property statement to include aViewController but it still errors. – Wez Mar 24 '13 at 23:14
  • sorry you should be using `_aViewController` – David Mar 24 '13 at 23:24
0
@property (nonatomic, retain) IBOutlet UILabel *labelupdate01;
@property (nonatomic, retain) IBOutlet UILabel *labelupdate02;

@property (nonatomic, assign) ViewController *aViewController;

Well, start with changing these properties to (nonatomic, weak) . This is the more correct approach. And it is not good practice to update display from one your model classes in MVC pattern. So your logic in + (void)updatedisplay doesn't make sense to me, I think it should be something like +(NSString *) getStringToDisplay with functionality implied in the func. name and label's text is updated in vc code based on the received string.

But just to answer your question in the way you asked:

The correct way is to do it as meth suggested or passing the viewcontroller to your newclass instance during initialization so your specific viewcontroller is already initialized.

As with most objects you don't get the currently active viewcontroller by doing:

 ViewController *labelupdate02;
 labelupdate02.labelupdate02.text = @"Update from NewClass";

labelupdate02 is uninitialized here. But even if you alloc and init it afterwards you are not going to get the active viewcontroller you want to update its view but just another viewcontroller instance.

guenis
  • 2,520
  • 2
  • 25
  • 37
  • Thanks for the information, I have changed them to weak and along with the suggestion below from Meth it is now working. – Wez Mar 24 '13 at 23:07