1

我刚刚添加了一个新的 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 以及我如何调用视图?

4

1 回答 1

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;
}
于 2013-10-27T00:34:51.963 回答