2

视图控制器.m

//
//  ViewController.m
//  WebViewDemo
//
//  Created by Vu Van Tiep on 1/15/13.
//  Copyright (c) 2013 Vu Van Tiep. All rights reserved.
//

#import "ViewController.h"
#import "WebViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"GoogleSegue"]){
        WebViewController *desView = (WebViewController*) [segue destinationViewController];
        UIWebView *webView = desView.webView;
        if([webView respondsToSelector:@selector(loadRequest:)] || webView != nil){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        //webView is nil

    }
}

@end

WebViewController.m

//
//  WebViewController.m
//  WebViewDemo
//
//  Created by Vu Van Tiep on 1/15/13.
//  Copyright (c) 2013 Vu Van Tiep. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()

@end

@implementation WebViewController
@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.scalesPageToFit = YES;
}

@end

WebViewController.h

@interface WebViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

当我使用 [segue destinationViewController] 时,我得到了 WebViewController 对象,但是当我访问该对象的 webView 属性时,它是 nil

有人可以向我解释一下吗?:(

谢谢!

4

1 回答 1

1

您的 WebViewController 的视图尚未初始化。你需要告诉它初始化。行后做

WebViewController *desView = (WebViewController*) [segue destinationViewController]; }

通过添加行

[desView view];

这与Use destinationViewController button in a custom segue 中的问题相同。虽然这一定是一个非常普遍的问题,但我发现很难找到有关此的信息。

于 2013-03-11T11:18:26.690 回答