0

I have a PasswordViewController that prompts me if I enter the incorrect username or password, but I can't get it to load my AdminViewController when the Enter button is pressed; it brings up a black screen.

PasswordViewController.h

#import <UIKit/UIKit.h>
#import "AdminViewController.h"

@interface PasswordViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UITextField *password;
- (IBAction)enterButton:(id)sender;

@end

NSDictionary *passwordDictionary;

PasswordViewController.m

#import "PasswordViewController.h"

@interface PasswordViewController ()

@end

@implementation PasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    passwordDictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"password", nil] forKeys:[NSArray arrayWithObjects:@"username", nil]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)enterButton:(id)sender {
    if ([[passwordDictionary objectForKey:_username.text]isEqualToString:_password.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Password" message:@"This password is correct." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        AdminViewController *secondView = [[AdminViewController alloc] init];
        [self presentViewController:secondView animated:YES completion:nil];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This password is incorrect" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
                [alert show];
    }
}

@end

AdminViewController.h

@interface AdminViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

AdminViewController.m

#import "AdminViewController.h"

@interface AdminViewController ()

@end

@implementation AdminViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSURL *myURL = [NSURL URLWithString:@"http://www.google.co.uk"];

    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];

    [_webView loadRequest:myRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
jscs
  • 63,694
  • 13
  • 151
  • 195
Elfuthark
  • 261
  • 1
  • 4
  • 17
  • When you say it brings up a black screen, that is indicating that it is actually loading the new view modally, but it seems the UIWebView isn't actually loading your url request. Try doing the request in viewWillAppear or viewDidAppear, and see if that helps any – user2277872 Feb 03 '14 at 16:42
  • i tried both as suggested and this time the black screen loads instantly when the app is run. When i remove the PasswordViewController and link the Admin button up directly using a Push Segue to the AdminViewController it works perfectly? Im stumped? – Elfuthark Feb 03 '14 at 18:03

2 Answers2

0

Your PasswordViewController needs to comply with the UIAlertViewDelegate

https://developer.apple.com/LIBRARY/ios/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

In this method:

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Is where you should launch your AdminViewController.

Tommy Alexander
  • 701
  • 1
  • 7
  • 17
0

Ok problem solved! I was ctrl+dragging from the Enter button on the PasswordViewController to the AdminViewController and selecting Push Segue instead of ctrl+dragging from the actual ViewController itself. I then had to setup a Segue Identifier. when i did this i was able to use the following code to get the desired results:

- (IBAction)enterButton:(id)sender {
    if ([[passwordDictionary objectForKey:_username.text]isEqualToString:_password.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Password" message:@"This password is correct." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [self performSegueWithIdentifier:@"passwordAccepted" sender:self];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This password is incorrect" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
    }
Elfuthark
  • 261
  • 1
  • 4
  • 17