简体   繁体   English

控制两个viewController之间的过渡

[英]Control the transition between two viewControllers

I am new to objective-c and have recently started making iOS applications in Xcode. 我是Objective-c的新手,最近开始使用Xcode制作iOS应用程序。 I was making a login application that has two view controllers. 我正在制作一个具有两个视图控制器的登录应用程序。 It looks like this: http://imgur.com/W3nxMEG 看起来像这样: http : //imgur.com/W3nxMEG

Now my issue is that I was the app to go to second ViewController only if: 1) Text fields are filled 2) Passwords match 现在我的问题是,只有在以下情况下,我才是进入第二个ViewController的应用程序:1)填写了文本字段2)密码匹配

that's the code that I've written. 那就是我写的代码。 I couldnt find any command that controls the transition between the viewControlled based on the certain constraints: 我找不到基于某些约束来控制viewControlled之间的过渡的任何命令:

(IBAction)loginButton:(id)sender {

BOOL passMatch, emptyUser, emptyPass, emptyrePass;

passMatch = [password isEqualToString:reEnter];

emptyUser = [username isEqualToString:@""];

emptyPass = [password isEqualToString:@""];

emptyrePass = [reEnter isEqualToString:@""];


if (emptyPass || emptyUser || emptyrePass){

    UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"You must complete all the fields " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [error show];

}

else{

    passMatch = [password isEqualToString:reEnter];

    if (passMatch){


       UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Passwords match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [pass show];

    } else{

        UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Passwords dont match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [error show];
    }
}

Shall be thankful if anyone can help me solve this 如果有人可以帮助我解决这个问题,请多谢

First make sure your UITextField and UIButton from your storyBoard are hooked with a property in LoginViewController.h 首先确保您的storyBoard中的UITextFieldUIButtonLoginViewController.h的属性挂钩

It'll look like this: 它看起来像这样:

LoginViewController.h LoginViewController.h

@interface LoginViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *password;

@property (strong, nonatomic) IBOutlet UITextField *username;

// and so on..

// for your button
@property (strong, nonatomic) IBOutlet UIButton *loginButton;

@end

LoginViewController.m LoginViewController.m

@implementation LoginViewController


-(IBAction)loginButton:(id)sender 
{

    // you can enable/disable your button here also, by:
    // UIButton *senderButton = (UIButton *)sender;
    // senderButton.enabled = YES/NO;

    // i'm just using .length because i trust numbers more that the @""
    NSString *errorMessage;

if (self.username.text.length == 0)
    errorMessage = @"username is required";

else if (self.password.text.length == 0)
    errorMessage = @"password is required";

else if (self.reEnter.text.length == 0)
    errorMessage = @"please confirm your password";

else if (self.password.text.length == 0 || self.username.text.length == 0 || self.reEnter.text.length == 0)
    errorMessage = @"You must complete all the fields";

if (errorMessage.length > 0)
{
    UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [error show];
}
else
{
    if ([self.username.text isEqual: @"youUsername"] && [self.password.text isEqual: @"youPassword"] && [self.password.text isEqual: self.reEnter.text])
    {
        UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Login successful" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [pass show];

        //presentYourSecondViewController         
        //[self presentViewController:(UIViewController) animated:(BOOL) completion:nil];
        //[self.navigationController pushViewController:(UIViewController)animated:(BOOL)];
    }
    else
    {
        UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Login failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [error show];
    }
}


    /* Your code...


    BOOL passMatch, emptyUser, emptyPass, emptyrePass;

    passMatch = [password isEqualToString:reEnter];

    emptyUser = [username isEqualToString:@""];

    emptyPass = [password isEqualToString:@""];

    emptyrePass = [reEnter isEqualToString:@""];

    if (emptyPass || emptyUser || emptyrePass){

        UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"You must complete all the fields " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [error show];

    }

    else
    {

        passMatch = [password isEqualToString:reEnter];

        if (passMatch){

            UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Passwords match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [pass show];

        } else{

            UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Passwords dont match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [error show];
        }
    }
    */
}

@end

There is nothing wrong with your condition i just over did it i guess.. Anyway hope i've help you Happy coding cheers.. 您的状况没有问题,我想我刚结束。.无论如何希望我能帮助您,为编码愉快而欢呼..

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

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