2013-10-21 92 views
0

我得到了一個問題,現在正在進行一個星期,我有一個webView,我設置了,但xib文件不想與我的控制器鏈接,我知道有一個話題關於這一點,但我是新的目標C和我不明白一個該死的事情,我寧願要以編程方式做,如果有人可以告訴我或請幫助我。Xib文件鏈接和WebViews

這裏是我的代碼:

.H #進口

@interface ViewController : UIViewController <UIWebViewDelegate> 
{ 
IBOutlet UIWebView *myWebView; 
} 

@property (nonatomic, retain) IBOutlet UIWebView *myWebView; 
-(void)PushView; 
@end 

.M

#import "MyWebViewController.h" 


@interface ViewController() 

@end 

@implementation ViewController 
@synthesize myWebView = _myWebview; 

-(void)viewDidLoad; 
{ 

    myWebView = [[UIWebView alloc] initWithFrame:self.view.frame]; 
    myWebView.delegate = self; 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *defaults = [prefs stringForKey:@"myKey"]; 
    NSString *defaults2 = [prefs stringForKey:@"mySecondKey"]; 

    NSString *username = defaults; 
    NSString *password = defaults2; 
    NSLog(defaults); 

    NSURL* url = [NSURL URLWithString:@"http://www.google.com"]; 

    NSString* body = [NSString stringWithFormat:@"login=%@&password=%@", username, password]; 
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30]; 
    request.HTTPMethod = @"POST"; 
    request.HTTPBody = [body dataUsingEncoding:NSStringEncodingConversionAllowLossy]; 

    [myWebView loadRequest:request]; 
    [super viewDidLoad]; 
} 

@end

回答

0

如果您Webview是conrrectly鏈接,你不」不需要再分配它。

你需要使用自我。訪問變量。使用self.mywebView或_mywebView,但myWebView指向一個空變量。

另外,在方法的開始處使用[super viewDidLoad]

-(void)viewDidLoad; 
{ 
    [super viewDidLoad]; 

    self.myWebView.delegate = self; 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *defaults = [prefs stringForKey:@"myKey"]; 
    NSString *defaults2 = [prefs stringForKey:@"mySecondKey"]; 

    NSString *username = defaults; 
    NSString *password = defaults2; 
    NSLog(defaults); 

    NSURL* url = [NSURL URLWithString:@"http://www.google.com"]; 

    NSString* body = [NSString stringWithFormat:@"login=%@&password=%@", username, password]; 
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30]; 
    request.HTTPMethod = @"POST"; 
    request.HTTPBody = [body dataUsingEncoding:NSStringEncodingConversionAllowLossy]; 

    [self.myWebView loadRequest:request]; 
} 

如果要檢查webview是否已鏈接,請在方法的開始處添加一個斷點並查看myWebView的值。

+0

現在就像一個魅力,謝謝先生! :d –