简体   繁体   中英

How to change the iVar of another class

This is the code. It is pretty straight forward. I made two classes one is returning the error and hydrate the iVar of another class (TheView) and show it to the User. however I cant figure it out why the View return Null at all time. Thanks is advance guys.

     @interface AccountControllerModel : NSObject {
            NSString *anError;
        }
        @property (nonatomic, retain) NSString *anError;

AccountControllerModel.m
    @synthesize anError;

    - (void)uploadFailed:(ASIHTTPRequest *)theRequest{

RegistrationViewController *regoVC = [[RegistrationViewController alloc] init];
[regoVC manageTheError:@"THIS IS AN ERROR"];
[regoVC release]; regoVC = nil;
    }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    @interface RegistrationViewController : UIViewController {
        NSMutableString *test;
    }
    @property (nonatomic, assign) NSMutableString *test;

@synthesize test;

-(void)viewDidLoad {
  test = [[NSMutableString alloc] init];
}
-(void)manageTheError:(NSString *)theError{
self.test = [NSMutableString stringWithFormat:@"%@",theError];
resultOfRegistration.text = [NSString stringWithFormat:@"%@",self.test];
NSLog(@"test is %@",self.resultOfRegistration.text); //It comes back Null
 }

Alex is right, some clarification on what's not working would help but by looking through I may have found your error. [[NSNotificationCenter defaultCenter] postNotificationName:@"Blah" object:self], you have object set to nil which could be your issue with the notification.

There are a number of problems with your code.

@property (nonatomic, assign) NSMutableString *test;

Two things here, one, exposing a NSMutable* object in a property is never a good idea, two you should 'copy' value objects, especially because this is how you're treating it in your code. Make this @property (nonatomic, copy) NSString *test;

regoVC.test = [NSMutableString stringWithString:self.anError];

You're assigning an autoreleased object to an assign property, this is a leak, the change above will fix that.

NSLog(@"test is %@",test); // It is perfect as you expect

test isn't in scope here, but I'd assume that was supposed to be regoVC.test , these other changes should remedy the situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM