0

一般来说,我对编程比较陌生,但我试图找到这个问题的答案,但一直没有结束。

所以基本上我正在尝试创建一个具有地图视图场景的应用程序,并且工作正常。然后我有另一个用于 Web 视图的场景。因此,当您进入 mapview 场景时,会有一个按钮将您带到 webview 场景。

然而,webview 场景根本不加载网页,尽管当我在不涉及其他编码的不同项目中创建应用程序的 webview 部分时,它工作得很好。

这是我到目前为止的编码:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController {
    MKMapView * mapview; IBOutlet UIWebView *webview;
}

@property (nonatomic, retain) IBOutlet UIWebView*webview;

@property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getlocation;



@end

--------


@implementation ViewController

@synthesize webview;
@synthesize mapview;


-(IBAction) getlocation {
    mapview.showsUserLocation= YES;
}

-(IBAction)setMap:(id)sender {
    switch(((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapview.mapType= MKMapTypeStandard;
            break;
        case 1:
            mapview.mapType= MKMapTypeSatellite;
            break;
        case 2:
            mapview.mapType= MKMapTypeHybrid;
            break;

        default:
            break;



    }




}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [webview loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.google.com"]]];
    // Do any additional setup after loading the view, typically from a nib.
}

如果您能提供帮助,将不胜感激。让我知道是否需要详细说明。

4

3 回答 3

0

以编程方式创建 webView:

添加viewDidLoad:

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.co.uk"]]];
                 [self.view addSubview:webView];
于 2013-05-12T23:33:06.427 回答
0

首先确保viewDidLoad正在执行。在某些情况下它不会被执行。

然后检查是否webviewnil里面viewDidLoad。这很可能是你的问题。您可能没有正确设置它,或者您在viewDidLoad执行后设置它。

最好的调试方法就是在代码中的 loadRequest 行设置一个断点,看看是否:

  1. 它完全到达你的断点
  2. webview 变量不是 0x00000000000

PS:你应该删除这些代码行:

MKMapView * mapview; IBOutlet UIWebView *webview;
-(IBAction)setMap:(id)sender;
@synthesize webview;
@synthesize mapview;

在最新版本的 Xcode 中不再需要它们,并且无论您从哪里学习它都是过时的。

另外,使用self.webview而不是仅仅使用webview. 直接访问它是危险的,并且可能导致错误。

于 2013-05-12T04:11:36.017 回答
0

请检查网络视图委托方法

[webview setDelegate:self];
于 2013-05-13T04:53:09.363 回答