簡體   English   中英

從警報視圖處理程序呈現新的視圖控制器將顯示黑屏

[英]Presenting new view controller from alert view handler displays black screen

我有一個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

您的PasswordViewController需要符合UIAlertViewDelegate

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

在這種方法中:

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

是您應該啟動AdminViewController的地方。

好的問題解決了! 我是從PasswordViewController上的Enter按鈕按Ctrl +拖動到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];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM