简体   繁体   中英

Calling instance method in iphone to set value causing crash by printing “(lldb)” in iOS Xcode

I am calling by button click in class SFAViewController

-(IBAction)uploadselector:(id)sender  // Notes and Attachments
{
    SFAUploadController *uploadcontroller = [[SFAUploadController alloc] initWithNibName:@"SFAUploadController" bundle:nil];
    //[SFAUploadController setRecordId:selectedrecordid];
    [self.view addSubview:uploadcontroller.view];
    [uploadcontroller setRecordId:selectedrecordid];
    NSLog(@"Okay uploading.. with record id %@",selectedrecordid);

}

which calls instance method in class SFAUploadController :-

-(void)setRecordId:(NSString *)value  // called by SFAViewController.m 
{
    self.receivedrecordid = [[NSString alloc]initWithString:@"Empty"];
    NSLog(@"Record value set");
    //receivedrecordid = value;
}

-(IBAction)selectfile:(id)sender // called when a button clicked in SFAUploadController class
{
    NSLog(@"Record in second class: %@",self.receivedrecordid); // causing program crash saying "(lldb)"
}

Please explain me why this happening? I am not so familiar with passing values reference from one class to another class in iphone.

Thanks.

you might be clicking on selectfile before receivedrecordid gets value? it can be good idea to check for nil before printing, it will avoid the crash, how you declare your receivedrecordid? you could make this property strong if you are using ARC otherwise retain , you should also check that you are connecting the IBAction for method selectfile properly for event touch up inside. From symptoms it looks like you are using an object which is not available or released/unallocated already. you should make sure all your objects and instances are being used in a proper way.

it is always good idea to use the viewController as property in other class so that it can be retained as long as corresponding class is alive.

in your particular case, as you are setting the value to the string only nothing big, then you could do that directly without using explicit method

uploadController.receivedRecordId=@"stirng you want to assign";

I think you don't need to create setter method for setting value to your receivedrecordid variable in SFAUploadController class.

Instead of creating setter method you can create @property for setting value to receivedrecordid like following:

SFAUploadController.h

@property (nonatomic, retain) NSSting *receivedrecordid;

And in SFAUploadController.m synthesize it like

@synthesize receivedrecordid;

Now you can directly use it like:

-(IBAction)uploadselector:(id)sender  // Notes and Attachments
{
    SFAUploadController *uploadcontroller = [[SFAUploadController alloc] initWithNibName:@"SFAUploadController" bundle:nil];
    [uploadcontroller setSelectedrecordid:@"recordId"];
    [self.view addSubview:uploadcontroller.view];
}

Hope it will help you.

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