Riddhi Riddhi - 3 years ago 271
iOS Question

Value is null in second view in objective-c

I am passing string between two views, my values are coming properly in first but in second view its null.

Here, I am using segue and string to pass artist name from one view to other.In first view the value is coming properly ,but in second view its null.

In first view

NSArray *artistnameArray = [value valueForKey:@"artistName"];
NSString *artistnameString = [artistnameArray objectAtIndex:indexPath.row];
UILabel *artistnameLBL = (UILabel *)[cell viewWithTag:102];
artistnameLBL.text = artistnameString;
valueToPassStr =artistnameLBL.text;
NSLog(@"ARTIST NAME : %@",artistnameLBL.text);

if ([valueToPassStr isEqualToString:@""])
NSLog(@"myString IS empty!");
artistnameLBL.text = @"";
NSLog(@" IS NOT empty, it is: %@", valueToPassStr);
LyricsViewController *lycVC= [[LyricsViewController alloc] initWithNibName:@"LyricsViewController" bundle:nil];
lycVC.valueToPassStr = artistnameLBL.text;
[lycVC setStrValue:valueToPassStr];
lycVC.strValue = valueToPassStr;
NSLog(@"VALUE IS PASSED : %@",lycVC.strValue);

In Second View

[super viewDidLoad];
LyricsViewController *lycVC = [[LyricsViewController alloc] init];

defaults = [NSUserDefaults standardUserDefaults];

// Do any additional setup after loading the view.
UILabel *artistnameLBL ;
[artistnameLBL setTag:105];
lycVC.strValue = strValue;

artistnameLBL.text =[NSString stringWithFormat:@"Hi! %@ ",strValue];
NSLog(@"ARTIST NAME : %@",artistnameLBL.text);

Answer Source
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
   if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    LyricsViewController *lycVC= (LyricsViewController *) segue.destinationViewController

For further information, please check the following link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download