[英]Presenting new view controller from alert view handler displays black screen
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; 我有一个PasswordViewController,如果我输入了错误的用户名或密码,会提示我,但是当按下Enter按钮时,我无法让它加载AdminViewController
。 it brings up a black screen. 它会显示黑屏。
PasswordViewController.h 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 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 AdminViewController.h
@interface AdminViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
AdminViewController.m 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
Your PasswordViewController needs to comply with the UIAlertViewDelegate 您的PasswordViewController需要符合UIAlertViewDelegate
https://developer.apple.com/LIBRARY/ios/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html https://developer.apple.com/LIBRARY/ios/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
In this method: 在这种方法中:
Is where you should launch your AdminViewController. 是您应该启动AdminViewController的地方。
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. 我是从PasswordViewController上的Enter按钮按Ctrl +拖动到AdminViewController,然后选择Push Segue而不是从实际ViewController本身按Ctrl +拖动。 I then had to setup a Segue Identifier. 然后,我不得不设置一个Segue标识符。 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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.