简体   繁体   English

在Xcode中按下按钮时如何去相同的视图控制器?

[英]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. 因此,我使用segues将值带到下一个View Controller。

If both text field values are null and then press login button means,I display an alert to fill both textfields. 如果两个文本字段的值都为null,然后按“登录”按钮,则显示警报以填充两个文本字段。 When pressing an OK button in alert the program AUTOMATICALLY goes to second controller. 在警报中按OK按钮时,程序将自动转到第二个控制器。 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. 更好的方法之一是使用未附加到动作的segue在情节提要中连接两个场景。 After that, you can programmatically trigger the segue inside your view controller. 之后,您可以以编程方式触发视图控制器中的segue。 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”; 将出现一个弹出窗口,要求在“ 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”. 接下来,您将使用一个按钮触发segue(或者您可以使用AlertView委托)。其选择器如“ 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()方法,并按如下方式调用segue:

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

no need of prepareForSegue 无需prepareForSegue

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

  { second *transferViewController = [segue destinationViewController]; 

    }

change your function into IBAction method 将函数更改为IBAction方法

  -(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 还设置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 - 我建议实现方法shouldPerformSegueWithIdentifier:此方法返回一个BOOL,指示是否应该继续进行搜索-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift 3中按下按钮时如何转到另一个视图控制器 - How to go to another view controller when button is pressed in swift 3 按下按钮后转到其他视图控制器 - On button pressed go to different view controller 如何在按下按钮时使按钮弹出警报视图并返回主视图控制器? - How Do I Make A Button Get An Alert View To Pop Up When Pressed And Go Back To The Main View Controller? 按下按钮时推送新的视图控制器 - Push new view controller when button is pressed 按下主页按钮时关闭视图控制器 - Dismiss view controller when the home button pressed 按下按钮时隐藏视图控制器/保留视图控制器的自定义配置-Xcode - Hide view controller when button pressed / Keep custom configuration of viewcontroller - Xcode Xcode:检测何时按下导航控制器实现的“后退按钮” - Xcode: Detecting when a navigation controller implemented “back button” is pressed 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 按下导航控制器后退按钮时如何导航到其他View Controller? - How to navigate to some other View Controller when Navigation Controller back button is pressed? 在其中一个控制器上按下按钮时,更改页面视图控制器中的视图 - Change view in a page view controller when button is pressed in one of the controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM