0

AdminViewController如果我输入了错误的用户名或密码,我有一个 PasswordViewController 会提示我,但是当按下 Enter 按钮时我无法加载它;它带来了一个黑屏。

密码视图控制器.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;

密码视图控制器.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
4

2 回答 2

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.

于 2014-02-03T21:41:01.013 回答
0

好的问题解决了!我是 ctrl+从 PasswordViewController 上的 Enter 按钮拖动到 AdminViewController 并选择 Push Segue 而不是 ctrl+从实际的 ViewController 本身拖动。然后我必须设置一个 Segue Identifier。当我这样做时,我能够使用以下代码来获得所需的结果:

- (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];
    }
于 2014-02-04T18:21:41.167 回答