1

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 = @"";
}
else
{
    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];
NSLog(@"VIEWDID LOAD CALLED");
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);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Riddhi
  • 39
  • 1
  • 10
  • Here `UILabel *artistnameLBL ;` you're defining a `UILabel` pointer which is **pointing to nil**. In Objective-C, when you send messages to `nil`, the program will simply **do nothing**, but `artistnameLBL` will point to nothing. Your second view will know nothing about the first view label (using the same name will only create a local variable which is different from the one in the first view controller). You have to use segues and public properties (defined in the header file) to actually pass values between view controller (or other harder methods which I wouldn't recommend to a beginner). – Alejandro Iván Oct 02 '17 at 22:09
  • It would be recommended for you to actually watch some videos about iOS programming (I especially recommend Stanford University ones, free on iTunes U). This is basic stuff that you need to learn before going any further. – Alejandro Iván Oct 02 '17 at 22:10
  • I tried doing that to, its not working. – Riddhi Oct 02 '17 at 22:12

1 Answers1

0
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
   {
    LyricsViewController *lycVC= (LyricsViewController *) segue.destinationViewController
    lycVC.strValue=valueToPassStr;
   }
}

For further information, please check the following link.

casillas
  • 16,351
  • 19
  • 115
  • 215
  • To avoid an Xcode warning, it would be preferrable to actually cast the destination view controller: `LyricsViewController *lycVC = (LyricsViewController *) segue.destinationViewController;` – Alejandro Iván Oct 02 '17 at 22:04
  • Hi, thx but its not workings think there is some issue in my passing value in secondVC,can you help me out with that? – Riddhi Oct 02 '17 at 22:06
  • Thanks Alejandro, I am responding on my phone. – casillas Oct 02 '17 at 22:07
  • try that one then `property(strong, nonatomic) NSString *srtValue` – casillas Oct 02 '17 at 22:07
  • Now I am getting value in ARTISTNAME1, but in nameLBL its null, – Riddhi Oct 02 '17 at 23:08
  • lycVC.valueToPassStr = strValue; NSLog(@"ARTIST NAME1 : %@",lycVC.valueToPassStr); nameLBL.text =lycVC.valueToPassStr; NSLog(@"ARTIST NAME : %@",nameLBL.text); – Riddhi Oct 02 '17 at 23:08