UIWebView不显示

时间:2013-10-26 23:58:42

标签: ios objective-c uiwebview

我刚刚添加了一个新的UIWebView,它似乎没有显示出来。我在这里很新,所以这可能是一个非常简单的错误。

我使用故事板创建了视图并将其拖入我的GuideViewController.h

这是我的GuideViewController.h

#import <UIKit/UIKit.h>

@interface GuideViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *newsWebView;

@end

这是我的GuideViewController.m

#import "GuideViewController.h"

@interface GuideViewController ()

@end

@implementation GuideViewController


@synthesize newsWebView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *newsURL = [NSURL URLWithString:@"http://www.yahoo.com"];
    NSURLRequest *newsRequest = [NSURLRequest requestWithURL:newsURL];
    [newsWebView loadRequest:newsRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这是我用来将视图添加到我的应用代理的内容。我已经添加了侧边栏菜单。

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    VidViewController *vidViewController = [[VidViewController alloc] init];

    GuideViewController *newsWebView = [[GuideViewController alloc] init];
    UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vidViewController];

[self.window setRootViewController:navController1];

    SelectVideo1 *selectVideo1 = [[SelectVideo1 alloc] initWith:@"Test"];
    //[self.navController pushViewController:selectVideo1 animated:YES];

    SelectVideo1 *selectVideo2 = [[SelectVideo1 alloc] initWith:@"Latest News"];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIStoryboard *tabStoryBoard = [UIStoryboard storyboardWithName:@"TabStoryboard" bundle:nil];
    UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:@"NavStoryboard" bundle:nil];
    UINavigationController *navController = [navStoryBoard instantiateViewControllerWithIdentifier:@"Nav Controller"];
    UITabBarController *tabController = [tabStoryBoard instantiateViewControllerWithIdentifier:@"Tab Controller"];


    ViewController *redVC, *greenVC;
    redVC = [[ViewController alloc] init];
    greenVC = [[ViewController alloc] init];



    RootController *menuController = [[RootController alloc]
                                      initWithViewControllers:@[navController, selectVideo1, selectVideo2, tabController, newsWebView]
                                      andMenuTitles:@[@"Test", @"Videos", @"Latest News", @"Walkthroughs", @"Official News"]];
    self.window.rootViewController = menuController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}



@end

视图显示在菜单栏中,但是当我点击它时,没有任何反应。我认为问题在于我的appdelegate以及我如何调用该视图?

1 个答案:

答案 0 :(得分:0)

如果您使用nib文件来定义GuideViewController,请使用

GuideViewController *newsWebView = [[GuideViewController alloc] initWithNibName:@"Insert_Nib_Name_Here" bundle:nil];

如果您在故事板中定义GuideViewController,请使用

UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:@"NavStoryboard" bundle:nil];
GuideViewController *newsWebView = [navStoryBoard instantiateViewControllerWithIdentifier:@"Insert_Guide_Controller_Name_Here"];

如果这不起作用,请确保您的IBOutlet UIWebView已连接(从nib文件中的UIWebView对象进行控制+拖动)

您也可以通过编程方式添加UIWebView。

在GuideViewController.h中,您可能希望符合UIWebViewDelegate协议。实现控制UIWebView功能的方法

@interface GuideViewController : UIViewController <UIWebViewDelegate>

在GuideViewController.m ...

- (GuideViewController*)init
{
    self = [super init];
    if (self) {
        //Adjust the frame accordingly if you don't want it to take up the whole view
        self.newsWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
        self.newsWebView.delegate = self;
        [self.view addSubview:self.newsWebView];
    }
    return self;
}