2010-07-14 56 views
0

我無法完成聽起來很簡單的事情。使用Interface Builder,我創建了一個UIWebView對象。接下來,我創建了一個新的控制器 - HomeViewController,並將其分配給「NIB Name」屬性下的WebView。 接下來,我需要在用戶訪問該視圖時將Web站點(假設爲http://google.com)加載到該WebView中。我無法找到任何有關如何實現這一目標的例子。請提供一個代碼示例。對於新手問題抱歉。我做了RTFM,但沒有運氣。帶有接口生成器的iPhone SDK UIWebView

回答

1

試試這個:

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]]; // webview - your Uiwebview object 
+0

能否請您對這個提供上下文?這個去哪裏以及「webview」對象來自哪裏?這是一個基於筆尖的UIWebView對象,那麼我如何在代碼中引用它? – user391204 2010-07-14 17:07:51

+0

在.h文件中爲UiWebview創建IBoutlet並將其連接到NIB文件中。 例如。 IBoutlet UiWebview * webview; – Reena 2010-07-15 03:46:58

2

.h文件:

@interface WebViewController : UIViewController { 
    IBOutlet UIWebView *webView; 
} 

@property (nonatomic, retain) UIWebView *webView; 

@end 

.m文件:

#import "WebViewController.h" 

@implementation WebViewController 

// Loads the url when the view loads 
- (void)viewDidLoad { 
    NSString *urlAddress = @"http://www.google.com"; 

    //Create a URL object. 
    NSURL *url = [NSURL URLWithString:urlAddress]; 

    //URL Request Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [webView loadRequest:requestObj]; 
} 

@end 
+0

這些引號混亂了,但我無法編輯帖子,因爲它至少需要編輯6個字符:/。複製貼紙,如果出現錯誤,請更改qoutes。除此之外,它完美的工作,+1。 – Kevin 2013-05-02 09:42:31

+0

我現在在使用您的代碼如何更改webview的字體???? – 2014-12-03 08:57:53