开发者

Why does my UIWebView not Allow User Interaction?

开发者 https://www.devze.com 2022-12-24 23:14 出处:网络
I\'m new to these forums so I apologize for my noobieness. I did as thorough a search as I could, but I couldn\'t find anyone else with this issue, applogise if this has been covered elsewhere.

I'm new to these forums so I apologize for my noobieness. I did as thorough a search as I could, but I couldn't find anyone else with this issue, applogise if this has been covered elsewhere.

I've created a very simple example of my problem. I'm sure I'm missing something but I can't for the life of me figure out what.

I'm creating a UIWebView and adding it to a custom view controller that inherits from UIViewController. When the app loads in the iPad simulator, the uiwebview loads the desired page, but the UIWebView is entirely unresponsive. The webview does not pan or scroll and none of the in page links can be clicked. However, if you change the orientation of the webview suddleny everything works.

Thanks in advance for your help!!

AppDelegate header


#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

View Controller header


#import <UIKit/UIKit.h>


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



@property (nonatomic, retain) UIWebView *webView;




@end

View Controller Implementation


#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)shouldAutorotateToInterface开发者_高级运维Orientation:(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


You should really be loading this from a nib, but if you're going to create it programmatically, you probably want to add this to loadView:

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


I'm going to go out on a limb here, as you aren't getting much response..

There is nothing wrong with this code (out on a limb! i haven't tested it). Neither is there anything wrong with not using a .nib (but really, you would have to have a reason, right?).

Using a nib doesn't do anything magic, just pretty much what you have done here.

Your problem must be elsewhere.. can you interact with a different kind of view? A button, say? Maybe it is something to do with the window setup?


The issue is with the extra UIView you have in there. It isn't needed, and I think the touch events aren't getting enabled. I tried out the code and it didn't work like you said. Then I changed the EditorViewController to this, and it works now (all I did was to remove the extra UIView) dragging and select links now works.

- (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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消