IOS Segue no puede configurar la etiqueta directamente

I am having two view controllers 'FirstViewController' and 'SecondViewController'. From first view controller it will take input from a text field ,process it and display accordingly in the second view. But I am having a problem while setting label value directly.

   @interface SecondViewController : UIViewController
    {
        NSString *numPlate;
        IBOutlet UILabel *output;
    };
    @property(strong,nonatomic) NSString *numPlate;
    @property(strong,nonatomic) IBOutlet UILabel *output;
    @end

The main file for FirstViewController.m with prepare for segue is as

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
    if([[segue identifier] isEqualToString:@"Change"])
    {
       SecondViewController *svc = (SecondViewController *)[segue            destinationViewController]; 
       svc.numPlate = input.text;


       NumberPlate *numPlate=[[NumberPlate alloc]init];
       [numPlate setPlate:input.text];
       NSInteger flag=[numPlate checkValidity];
       if(flag==0)
       {
           svc.output.text  =@"Invalid License";
       }
       else
       if([numPlate getArea]==NULL||[numPlate getRegOffice]==NULL)
       {
            svc.output.text  =@"Data not found";
       }
       else
       {
        svc.output.text  =@"VALID License";
       }
     }
   }

But when the action is performed its not working.The label is not changing. When i used svc.numPlate instead of svc.output.text and in the SecondViewController viewDidLoad method and i used

 - (void)viewDidLoad
{
    [super viewDidLoad];
     output.text=numPlate;
}

Everything is fine with this. Whats wrong in first method??

preguntado el 27 de noviembre de 13 a las 06:11

3 Respuestas

You will not be able to assign value directly to UILabel of second VC as view is not yet loaded into view hierarchy en este punto.

So view cannot render the value assigned prior to it.

On the other hand, holding value in NSString and assigning same on viewDidLoad is working as now your view is in view hierarchy and loaded into memory.

respondido 27 nov., 13:06

Thanks,Is there any better method than passing data to a string. - esclavoCoder

@BittuAby in a way this is the solution rather having globals or static for passing value. - βhargavḯ

I am new to ios programming. Can you give me some tutorial links on these topics. ie globals /statics - esclavoCoder

At the time when you push SecondViewController, the SecondViewController's view hasn't been loaded yet, so you can't access its views. You need to create NSString properties in SecondViewController and pass a string to SecondViewController' NSString Object. Then in SecondViewController's viewDidLoad method, use those properties to populate the labels (which will have been loaded by the time viewDidLoad runs).

respondido 27 nov., 13:06

The initialisation of controller is different and presentation of its view is different process even if the viewController has been initialise its view will not because pushing the controller has not performed and controller dont know he need to load the view.. so we pass the data to controller and it load when view appears... like below code

@interface SecondViewController : UIViewController{
   NSString *strMessage;

}
@property(nonatomic,retain) NSString *strMessage;

@end
SecondViewController.m

@synthesize strMessage;

- (void)viewDidLoad {
Nslog(@"%@",strMessage);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if([[segue identifier] isEqualToString:@"Change"])
     {
      SecondViewController *svc = (SecondViewController *)[segue          destinationViewController]; 


    NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
    svc.strMessage=message;
}

respondido 27 nov., 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.