IOS ViewController UILabel update from another class

I am new to development and banged my head against the wall, trying to figure it out, I’m sure that I’m missing something stupid, but after I tried different solutions, I still can’t get the result I'm looking for.

I would like to be able to update the UILabel in the ViewController from another class, here is a small demo program that I can’t work with, I have a ViewController that has two UILabels, one of which is updated with viewDidDoad and the other that I would like update from another class NewClass, which is called from ViewController , I see that the class is correctly called, because the console registers the NSLog record, but I can not get the syntax for updating 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 
+4
source share
3 answers

define this in newClass as

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

and name it from your viewController

 [NewClass updatedisplay:self]; 
+4
source

First make sure that you initialize the 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 
+1
source
 @property (nonatomic, retain) IBOutlet UILabel *labelupdate01; @property (nonatomic, retain) IBOutlet UILabel *labelupdate02; @property (nonatomic, assign) ViewController *aViewController; 

Well, start by changing these properties to (non-atomic, weak). This is a more correct approach. And it is not recommended to check the display of one of the model classes in the MVC template. So, your logic in + (void)updatedisplay does not make sense to me, I think it should be something like +(NSString *) getStringToDisplay with the functionality implied in func. label name and text are updated in vc code based on the received string.

But just to answer your question the way you asked:

The right way is to do this as suggested or passed the viewcontroller to your newclass instance during initialization, so that your specific view manager is already initialized.

As with most objects, you do not get the active active view manager, doing:

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

labelupdate02 is not initialized here. But even if you subsequently select and run it, you will not get the active view controller that you want to update, but just another instance of the viewcontroller.

0
source

All Articles