[英]How to go to another view controller when button is pressed in swift 3
[英]how to go same view controller when button pressed in Xcode?
我正在开发一个登录应用程序,并且我有一些疑问。 如果按下登录按钮,则该文本字段中的任何文本都应在下一个视图控制器中显示-如用户名和密码。 因此,我使用segues将值带到下一个View Controller。
如果两个文本字段的值都为null,然后按“登录”按钮,则显示警报以填充两个文本字段。 在警报中按OK按钮时,程序将自动转到第二个控制器。 当按下按钮时,我不知道如何去使用相同的视图控制器。 请帮助我。
我的代码:
-(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];
}
}
有很多不同的方法可以做到这一点。 更好的方法之一是使用未附加到动作的segue在情节提要中连接两个场景。 之后,您可以以编程方式触发视图控制器中的segue。 您可以通过控制方式将故事板场景底部的文件所有者图标拖动到目标场景。
将出现一个弹出窗口,要求在“ Manual Segue”中进行选择; 选择“推送”作为类型。 点击小方块,确保您位于“属性”检查器中。 给它一个标识符,您将在代码中使用它来引用它。 接下来,您将使用一个按钮触发segue(或者您可以使用AlertView委托)。其选择器如“ buttonAction”。 如果按下按钮,将调用此功能。此功能可触发搜索并转到目标场景。 因此,该按钮并在该方法内使用void()方法,并按如下方式调用segue:
-(void)buttonAction:(id)sender{
if(textfield.text.length!=0){
[self performSegueWithIdentifier:@"MySegueName" sender:sender];
}
else{
alert!!
}
}
无需prepareForSegue
-(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender
{ second *transferViewController = [segue destinationViewController];
}
将函数更改为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];
}
}
尝试这个....
- (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];
}
}
还设置UIAlertViewDelegate
@interface YourViewController : UIViewController <UIAlertViewDelegate> //in which you used `UIAlertView`
我建议实现方法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.