Xib文件链接和WebViews

时间:2013-10-21 08:26:03

标签: ios objective-c xib

我遇到了一个问题,现在已经持续了一个星期,我有一个我设置的webView,但xib文件不想与我的控制器链接,我知道有一个话题,但是我我对目标c不熟悉,我不明白该死的东西,如果有人可以给我看或帮助我,我宁愿以编程方式进行。

这是我的代码:

.H     #import

@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

1 个答案:

答案 0 :(得分:0)

如果您的网页浏览链接正确,则无需再次分配。

你需要使用自我。访问变量。使用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的值。