0

我遇到了一个持续一周的问题,我设置了一个 webView,但是 xib 文件不想与我的控制器链接,我知道有一个关于这个的话题,但我是新手目标c,我什么都不懂,如果有人可以向我展示或帮助我,我宁愿以编程方式进行。

这是我的代码:

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

@结尾

4

1 回答 1

0

如果您的 webview 已正确链接,则无需再次分配它。

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

于 2013-10-21T08:28:24.327 回答