覆盖loadView并以编程方式设置rootViewController,没有Storyboard

时间:2014-11-22 01:20:01

标签: ios objective-c uiwebview programmatically-created

我正在尝试以编程方式创建View作为根View。我没有使用故事板或ARC。

我正在尝试将UIWebView设置为ViewController'sView,我无法做到。当我运行以下代码时,我看到一个空白屏幕NavigationBar。我尝试将常规UIView设置为根View并将其背景颜色设置为蓝色,然后加载,因此问题似乎特定于UIWebView。 如果有人能指出我做错了什么,我真的很感激。谢谢!

这是我的loadView代码:

// in GmailOAuthViewController.m
- (void)loadView {

    CGRect frame = [[UIScreen mainScreen] bounds];
    UIWebView *webView = [[[UIWebView alloc] initWithFrame:frame] autorelease];

    self.view = webView;
}

这是我的application:didFinishLaunchingWithOptions:代码:

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

    CGRect frame = [[UIScreen mainScreen] bounds];
    UIViewController *viewController = [[GmailOAuthViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    self.window = [[UIWindow alloc] initWithFrame:frame];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

1 个答案:

答案 0 :(得分:1)

你正在分配自我... 如果你指定webView来查看其他一切都消失了 试试

[self.view addSubview: webView]    

这是我刚尝试的代码,它可以正常工作

CGRect frame = [[UIScreen mainScreen] bounds];
    UIViewController *viewController = [[ViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    self.window = [[UIWindow alloc] initWithFrame:frame];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
ViewController.m中的

-(void)viewDidAppear:(BOOL)animated{
    CGRect frame = self.view.bounds;
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame] ;
    webView.delegate = self;
    self.view = webView;

    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"ViewControllerCatalog" ofType:@"pdf"];
    if (thePath) {
        NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
        [(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"
                        textEncodingName:@"utf-8" baseURL:nil];
    }

}

我很抱歉我早先告诉你错了你实际上已经分配到了