1

I am trying to move to a page after a successful message from my server for a basic login application. Currently when I get a successful log it moves to a black screen like its looking a view controller thats not there. However there is a view controller accioated with the class i am trying to go to. Here is my code so far

    if([serverOutput isEqualToString:@"Yes"])
    {
        NSLog(@"Yes there was a match");
        WelcomeScreenViewController *newview =[[WelcomeScreenViewController alloc] init];
        [self presentViewController:newview animated:YES completion:nil];
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];


        [alertsuccess show];
       `}

Why isn't after i successfully log in it taking me to the welcome screen? Do I need to make the welcome the rootcontroller and then push the log-in screen. Why can't I programmatically move to the view controller with an if statement?

`- (IBAction)loginbuttonpressed:(id)sender {

//Trim white space off username/password
NSString *rawEmail = [_UIEmailTextField text];
NSString *rawPass = [_UIPasswordTextField text];
NSString *trimmedEmail = [rawEmail stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *trimmedPass = [rawPass stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

//Check if text field is empty
if([_UIEmailTextField.text length] > 0 && [_UIPasswordTextField.text length] > 0)
{
    NSString *post =[NSString stringWithFormat:@"userName=%@&userPassword=%@",trimmedEmail, trimmedPass];
    NSString *hostStr = @"http://server name?";
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
    if([serverOutput isEqualToString:@"Yes"])
    {
        NSLog(@"Yes there was a match");
        WelcomeScreenViewController *newview =[[WelcomeScreenViewController alloc] init];
        [self presentViewController:newview animated:YES completion:nil];
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];`


    So all of this is run when you hit a log in button. I had it being a seque when you click log in but then if the log in fails it still logs in. 
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Slowbro
  • 189
  • 3
  • 13

1 Answers1

2

When testing for a value prior to a segue you should impliment a segue from the viewController rather than from the UIButton. You then use performSegueWithIdentifier to execute the segue you added to the controller. Then in your prepareForSegue you can test for different segues and pass things to the destinationViewController.

Perform Segue programmatically and pass parameters to the destination view

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • So would that replace the line `WelcomeScreenViewController *newview =[[WelcomeScreenViewController alloc] init];` – Slowbro Apr 22 '13 at 20:55
  • Yes. But if you are saying the view changes regardless then it sounds like you are using a segue as well. You can set the bundle to nil but the nib name needs to be set. – Mark McCorkle Apr 22 '13 at 20:55
  • I am using a seque. Is there a way to cancel a seque way even if the button is pressed if some other action isn't met? – Slowbro Apr 22 '13 at 20:59
  • Yes, in the case of a segue, remove the segue from the button and create one from the UIViewController. Then in your IBAction run [self performSegueWithIdentifier@""]; Now you need to implement the prepareForSegue method if you are wanting to pass anything to the new view. – Mark McCorkle Apr 22 '13 at 21:02
  • Check this out. http://stackoverflow.com/questions/9248798/perform-segue-programmatically-and-pass-parameters-to-the-destination-view – Mark McCorkle Apr 22 '13 at 21:04
  • 1
    MarkM you are truly a hero! – Slowbro Apr 22 '13 at 21:09
  • Your welcome! Mark the question answered if you are satisfied with the answer. Enjoy and good luck! – Mark McCorkle Apr 22 '13 at 21:22