从警报视图处理程序呈现新视图控制器显示黑屏

时间:2014-02-03 15:51:12

标签: ios objective-c cocoa-touch uialertview uivewcontroller

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

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

2 个答案:

答案 0 :(得分:0)

您的PasswordViewController需要符合UIAlertViewDelegate

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

在这种方法中:

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

您应该在哪里启动AdminViewController。

答案 1 :(得分:0)

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

- (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];
    }