简体   繁体   中英

String become nil after viewDidLoad with protocol and delegate

I have a protocol and delegate and I have it send a string then change the Tab on a UItabbar, My protocol and delegate works just find and it sets a string in the viewcontroller but as soon as the view finishes loading it becomes nil Here is my code;

-(void)duplicateAsset:(NSString *)serialNumber{

    [self setDubString:serialNumber];
    NSLog(@"delegate called DubString = %@",self.dubString);

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"_dubString in viewDidLoad = %@",self.dubString);
}

Output

 delegate called DubString = SERIALNUMBER
 _dubString in viewDidLoad = (null)

I'm not sure whats going on because I have done this many times before.

EDIT: I updated what the log says. I have set the string properties to copy and to strong and I still can't get the string to stay

In the code shown you do not actually give dubString as argument to NSLog , is this the actual code you're using? If yes, then that's the problem, add , self.dubString before the closing ) of NSLog . If not, then check that the dubString property is set to strong or copy (or, if it's not a property, that your setDubString: method takes ownership of the string or copy thereof).

edit : There doesn't seem to be anything wrong with the code shown, so if you are unable to trace the bug, post more code (eg, all contexts where dubString is set, such as the place where you are calling duplicateAsset ).

Meanwhile I would suggest checking that you are calling duplicateAsset on the same object that has its viewDidLoad called (include the object id of self in both debug printouts). Also ensure that nothing else sets dubString in between, eg, define the setDubString: accessor yourself and add a debug printout (or breakpoint) there.

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