custom segue help...

Discussion in 'iOS Programming' started by shahab47, Apr 8, 2012.

  1. shahab47, Apr 8, 2012
    Last edited by a moderator: Apr 10, 2012

    shahab47 macrumors newbie

    Joined:
    Apr 8, 2012
    #1
    Hello there.

    I am very new to apple/mac.
    I am designing a simple app, where I check the username and password and if that matches, it would open a new page. It is probably very simple. Its just that I am new to this. So I don't know.

    Everything works fine except that I cannot put a condition in the custom segue which checks if username and password are correct or not, before loading the next page.

    How can I do that?

    This is my custom segue: (I need to add a condition where I can check if username and password in the source view controller are correct or not before loading destination view controller.

    I am attaching my entire project in zip file. Here.

    Thanks
    Shahab

    Code:
    #import "Akire_login_customsegue.h"
    
    @implementation Akire_login_customsegue
    -(void)perform
    {
        [self.sourceViewController presentModalViewController:self.destinationViewController animated:YES];
    }
    @end
     

    Attached Files:

  2. Sykte, Apr 9, 2012
    Last edited by a moderator: Apr 10, 2012

    Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #2
    I honestly do not think a segue is a proper location to verify credentials. I would run a conditional check in the source viewcontroller then only push the segue if the credentials pass.

    However to answer your question you can access the source and destination properties via self.sourceViewController & self.destinationViewController.

    Hope this helps.
     
  3. shahab47, Apr 9, 2012
    Last edited by a moderator: Apr 10, 2012

    shahab47 thread starter macrumors newbie

    Joined:
    Apr 8, 2012
    #3
    Hello Skyte.

    Thanks a lot for your answer.

    I think I am doing the credential check in the source view controller only. Once the credential match, I print out a label saying"login successful" or "login failed". And I can do that successfully.

    Now after this text label is displayed, I want the screen to show up 2nd view controller. For this, I don't know any other way. I don't want to use "nav bar"

    so once I click the "submit" button, this action checks the credentials:
    Code:
    - (IBAction)alogin:(id)sender {
        
        //Store the TF text into Button var declared in .h file
        self.B_login = self.TF_username.text;
        NSString *uname_var = self.B_login;
        //Store the TF text into Button var declared in .h file
        self.B_login = self.TF_password.text;
        NSString *pword_var = self.B_login;    
        //We cannot use this above variable anywhere by itself.
        //So we assign this to another variable.
        
        //Initialize something. need to explain later
        NSString *result_pass_var = [[NSString alloc] initWithFormat:@"Login Success"];
        NSString *result_fail_var = [[NSString alloc] initWithFormat:@"Login Failed!"];
        NSString *username_input = [[NSString alloc] initWithFormat:@"shahab"]; 
        NSString *password_input = [[NSString alloc] initWithFormat:@"inamdar"];
        
        
        NSString *result_var = [[NSString alloc] initWithFormat:@"%@ %@", uname_var,pword_var];
        self.LB_result.text = result_var;
        
        if ([uname_var isEqualToString:username_input] && [pword_var isEqualToString:password_input])   
            self.LB_result.text = result_pass_var;
        else 
            self.LB_result.text = result_fail_var;
    }
    Now I tried to access "self.sourceviewcontroller.LB_result.text == bla bla" in my custom segue, but it gives error. (LB_result doesn't show up when I hit the dot after self.sourceviewcontroller).

    So in above code, I checked the credential. Now I should push the screen to destination controller. Could you please suggest, how do I do that? What code should I add above so that destination view controller opens up?

    I am very new to this. ANy help would be appreciated.
    Thanks
    Shahab
     
  4. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #4
    Hi Shahab47,

    You should do some reading on how to cast objects. However, I have posted two examples below on how to accomplish your goal.

    Code:
    myViewControllerName *loginViewController = (myViewControllerName *) self.sourceViewController
    
    
     //Now you can access your property.
    loginViewController.LB_result.text
    
    or

    Code:
    //You may cast an object and access your property all within one line of code
    //I have to say I typically prefer the option above.
    ((myViewControllerName *)self.sourceViewController).LB_result.text
    
    Hope this helps. I'm heading out shortly but I will try and give you an example on how to handle everything in your source(login)ViewController when I get back.

    Good Luck.
     
  5. shahab47, Apr 10, 2012
    Last edited by a moderator: Apr 11, 2012

    shahab47 thread starter macrumors newbie

    Joined:
    Apr 8, 2012
    #5
    Hello Skyte.

    Your suggestion worked.
    I can now log in. And the credential check is done in the custom segue though.
    Thank you so much.

    I remember you told me that its not a good idea to check credentials in custom segue.
    So I did a little research and decided to do the credential check in UIViewcontroller file only.

    So I created a new UIVIewController Object and named it as loginafter_viewcontroller.m. Now when I check credentials in original UIVIewcontroller then if credentials check out, I need to call this loginafter_viewcontroller.

    And I couldn't do that. It opens a black page instead of my "loginafter_viewcontroller". the project doesn't crash but its black screen.

    Here is my code:
    Code:
    if username and password equal to bla and bla, open "loginafter_veiwcontroller.
        
        if ([uname_var isEqualToString:username_input] && [pword_var isEqualToString:password_input]) 
        {
    
            loginafter_ViewController *myNewVC = [[loginafter_ViewController alloc] init];        
            [self presentModalViewController:myNewVC animated:YES];
    If you find some time, please explain to me how to do that. I think I am again missing a statement above.
    Thanks again a lot
    Shahab
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State

Share This Page