为什么我的UIWebView不允许用户交互?

时间:2010-03-30 19:33:43

标签: objective-c ipad uiwebview

我是这些论坛的新手所以我为自己的诺言而道歉。我尽可能彻底地进行了搜索,但是我找不到其他任何有这个问题的人,如果在其他地方已经覆盖了这个问题,那就是applogise。

我已经创建了一个非常简单的问题示例。我确定我错过了一些东西,但我不能为我的生活弄清楚是什么。

我正在创建一个UIWebView并将其添加到继承自UIViewController的自定义视图控制器。当应用程序在iPad模拟器中加载时,uiwebview会加载所需的页面,但UIWebView完全没有响应。 webview不会平移或滚动,也无法单击任何页面链接。但是,如果你改变webview的方向,那么一切都会有效。

先谢谢你的帮助!!

AppDelegate标题


#import <UIKit/UIKit.h>
#import "EditorViewController.h"

@interface FixEditorTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    EditorViewController *editorView;
}



    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) EditorViewController *editorView;

@end

AppDelegate Implementation


#import "FixEditorTestAppDelegate.h"
#import "EditorViewController.h"



@implementation FixEditorTestAppDelegate



@synthesize window;
@synthesize editorView;




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    NSLog(@"application is loading");
    editorView = [[EditorViewController alloc] init];
    [window addSubview:[editorView view]];
    [window makeKeyAndVisible];
    return YES;
}




- (void)dealloc {
    [window release];
    [editorView release];
    [super dealloc];
}
@end

查看控制器标题


#import <UIKit/UIKit.h>


@interface EditorViewController : UIViewController <UIWebViewDelegate> {
    UIWebView *webView;
}



@property (nonatomic, retain) UIWebView *webView;




@end

查看控制器实施


#import "EditorViewController.h"



@implementation EditorViewController
@synthesize webView;



/*
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/




// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    NSLog(@"loadView called");

    UIView *curView = [[UIView alloc] init];

    webView = [[UIWebView alloc] init];
    webView.frame = CGRectMake(20, 40, 728, 964);
    webView.delegate = self;
    webView.backgroundColor = [UIColor redColor];

    [curView addSubview: webView];
    self.view = curView;

    [curView release];



}







//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad called");
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.nytimes.com"];



    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [webView loadRequest:request];
    [url autorelease];
    [request release];



}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}




- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}




- (void)viewDidUnload {
    webView.delegate = nil;
    [webView release];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (void)dealloc {
    [super dealloc];
}




@end

3 个答案:

答案 0 :(得分:2)

你应该从一个nib加载它,但是如果你要以编程方式创建它,你可能想要将它添加到loadView:

webView.userInteractionEnabled = YES;
webView.scalesPageToFit = YES;

答案 1 :(得分:0)

我会在这里走出困境,因为你没有得到太多回应..

这个代码没有任何问题(在肢体上!我还没有测试过它)。不使用.nib也没有任何问题(但实际上,你必须有理由,对吧?)。

使用笔尖不会做任何魔术,只是你在这里做的很多。

您的问题必须在其他地方..您可以与其他类型的视图互动吗?一个按钮,说?也许它与窗口设置有关?

答案 2 :(得分:0)

问题在于你在那里的额外UIView。它不是必需的,我认为触摸事件没有启用。我尝试了代码,它没有像你说的那样工作。然后我将EditorViewController更改为此,它现在正常工作(我所做的就是删除额外的UIView)拖动并选择链接现在可以工作。

- (void)loadView {
    NSLog(@"loadView called");

    webView = [[UIWebView alloc] init];
    webView.frame = CGRectMake(20, 40, 728, 964);
    webView.delegate = self;
    webView.backgroundColor = [UIColor redColor];

    self.view = webView;
}