简体   繁体   中英

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]; 
    } 
} 

There are many different ways to do it. One of the nicer ways to do this is by connecting two scenes in a storyboard using a segue that is not attached to an action. After that, you can programmatically trigger the segue inside your view controller. The way you do this is by control-dragging from the file owner icon at the bottom of the storyboard scene to the destination scene.

A popup will appear that will ask for an option in “Manual Segue”; pick “Push” as the type. Tap on the little square and make sure you're in the Attributes Inspector. Give it an identifier which you will use to refer to it in code. Next, you are going to trigger the segue using a button(or can u can use the AlertView delegate ) .That have the selector like “buttonAction”. This is the function that will be called if you press the button.this function to trigger the segue and go to the destination scene. So a void() method for that button and within that method and call the segue like this:

-(void)buttonAction:(id)sender{
 if(textfield.text.length!=0){
    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
  }
 else{
     alert!!
   }
}

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]; 
        }
     }

try this....

- (IBAction)loginButtonPressed:(UIButton *)sender
{
    if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ])
    {
        UIAlertView * mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" message:@"This is the Iphone app" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [mes show];
    }
    else
    {
        [self performSegueWithIdentifier:@"yoursegueID" sender:nil]; // 
    }

}



- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{

    if (buttonIndex == 0)
    {
        [self performSegueWithIdentifier:@"yoursegueID" sender:nil];
    }
}

also set UIAlertViewDelegate

@interface YourViewController : UIViewController <UIAlertViewDelegate> //in which you used `UIAlertView`

I would suggest implementing the method shouldPerformSegueWithIdentifier: This method returns a BOOL that indicates whether the segue should proceed -

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{ 

  BOOL ret=YES:

  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]; 
    ret=NO;
  } 

  return ret;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM