简体   繁体   English

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

[英]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: 在这种方法中:

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

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.

相关问题 呈现视图控制器时出现黑屏 - Black screen when presenting view controller 当前视图控制器显示黑屏 - Present view controller displays black screen 从选项卡控制器在当前上下文中显示模型视图控制器后出现黑屏 - Black screen after presenting model view controller in current context from tab controller 以编程方式显示没有标识符的视图控制器会导致黑屏 - Presenting view controller without identifier programmatically results in black screen 从 UITabBarController 在当前上下文中呈现模态视图控制器后的黑屏 - Black screen after presenting modal view controller in current context from UITabBarController 解散View Controller,然后显示一个Alert Controller - Dismissing a View Controller, then presenting an Alert Controller UISegue不提供新的视图控制器 - UISegue is not presenting new view controller 从呈现视图控制器推送视图控制器 - Push View Controller from Presenting View Controller 从AppDelgate呈现View Controller - presenting View Controller from AppDelgate 当呈现另一个视图控制器时,如何阻止“ from”视图变黑? - How can I stop the “from” view from becoming black when presenting another view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM