Gopi Krish Gopi Krish - 5 months ago 21
Objective-C Question

how to go same view controller when button pressed in Xcode?

I am developing a login app, and I have some queries. if the login button is pressed, whatever text is in that textfield should be displayed in next view controller - like username and password. So I used segues to take values to next view Controller.

If both text field values are null and then press login button means,I display an alert to fill both textfields. When pressing an OK button in alert the program AUTOMATICALLY goes to second controller. I don't know how to go same view controller when button pressed. kindly help me.

My code:

-(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender
{
second *transferViewController = [segue destinationViewController];
if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ])
{
UIAlertView * mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" message:@"This is the Iphone app" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[mes show];
}
}

Answer

no need of prepareForSegue

 -(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender

  { second *transferViewController = [segue destinationViewController]; 

    }

change your function into IBAction method

  -(IBAction)login:(id)sender
  {

     if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ]) 
     { 
       UIAlertView *mes=[[UIAlertView alloc] initWithTitle:@"Empty Fields !!!" message:@"enter the user details" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [mes show];

    }

   else

    {

        // both textfield are filled and validated
          [self performSegueWithIdentifier:@"your segue name of secondVC" sender:sender]; 
        }
     }