2014-11-22 64 views
0

我試圖以編程方式創建並設置View作爲根View。我沒有使用Storyboard或ARC。以編程方式覆蓋loadView並設置rootViewController,沒有Storyboard

我試圖設置一個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

如果分配web視圖以查看其他一切都是你賦予self.view ... 不見了 嘗試

[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]; 
    } 

} 

我很抱歉,我告訴你錯在先你實際上分配到查看

+0

嗨,爲感謝您的響應。如果我進行更改,屏幕永遠不會從版權屏幕轉換。除了'UIWebView',我沒有創建父視圖,也沒有使用Storyboard。我想將它設置爲'View'的根目錄並以編程方式顯示它。 – Impossibility 2014-11-22 02:01:14

+0

使用UInavigationController * navi ...而不是self.nav ... – 2014-11-22 02:29:51

+0

上面的代碼工作,我只是藥物與該名稱的PDF到Xcode和一切都很好。我猜你的導航控制器屬性干擾了現有的屬性 – 2014-11-22 02:48:26

相關問題