Passing NSString to UILabel using storyboard segues

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   NSLog(@"prepareForSegue: %@", segue.identifier);
   if([segue.identifier isEqualToString:@"returnText"])
   {   
      [segue.destinationViewController setText:@"New String"];
   }
}


-(void)setText:(NSString*)transferString;
{
   NSString* result = [NSString stringWithFormat:@"%@", transferString];
   NSLog(@"Got transfer %@", result);
   //Prints correct string
   LabelText.text=result;
   NSLog(@"Labeltext %@\n",LabelText.text);
   //NSLog outputs null
   //Doesn’t update the label
 }

      

I want the label to update with new text after switching ...

+3


source to share


1 answer


Try the following:

// In your destination controller .h file
@property (nonatomic, copy) NSString *transferStr;

// In your destination controller .m file
@synthesize transferStr;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.LabelText.text = self.transferStr;
}

// In your first controller .m file
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   NSLog(@"prepareForSegue: %@", segue.identifier);
   if([segue.identifier isEqualToString:@"returnText"])
   {   
      [segue.destinationViewController setTransferStr:@"New String"];
   }
}

      

Tested and working ...



Don't forget, however, to include the final controller file .h in your first controller.

Probably the problem is that the UILabel is not created yet when the prepareForSegue method is called, so at this particular point in time it is zero.

+8


source







All Articles