开发者

presentModalViewController fails to show up

开发者 https://www.devze.com 2023-04-02 08:51 出处:网络
To begin with, I am new to the Objective C world, so my question might sound naive. Even the structuring of methods might not be too proper, so kindly bear with me. I am sure I will learn as I move al

To begin with, I am new to the Objective C world, so my question might sound naive. Even the structuring of methods might not be too proper, so kindly bear with me. I am sure I will learn as I move along.

I have a container view controller "DeckViewController" which houses another view controller "CardDetails". CardDetails has a scroll view which is used to load 3 custom UIViews (CustomWebView) at a time, in which HTML is loaded. The HTML loaded in these views contains images and the requirement is that once the user clicks on any image in the page then it opens in a QLPreviewController. I have wrapped all the images in the HTML in an anchor tag to ensure that shouldStartLoadWithRequest delegate handles the loading of that image.

This handler is written in the class CustomWebView and I am trying to load the preview controller using CustomWebView's parent view controller i.e. CardDetails.

I have traced the code and it is producing the right values but some how the modal view controller is not coming up at all. I might just be missing a small link or missing the whole point here. Would highly appreciate help and guidance.

Code snippet for the shouldStartLoadWithRequest delegate handler is given below-

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* strExtension;
NSLog(@"THE Path of the image %@",request);
NSString *imageName = [[[request URL] path] lastPathComponent];
NSArray *comt = [imageName componentsSeparatedByString:@"."];
strExtension = [comt objectAtIndex开发者_开发问答:[comt count]-1];
NSLog(@"file extension %@",strExtension);
if ([strExtension isEqualToString:@"png"] || [strExtension isEqualToString:@"jpeg"] || [strExtension isEqualToString:@"jpg"]) {
    NSLog(@"######v %@",[[request URL] path]);
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    ImageViewController *imagecontrller = [[ImageViewController alloc] init];
    previewController.dataSource = imagecontrller;
    previewController.delegate = imagecontrller;
    [imagecontrller setImageURL:[request URL]];
    previewController.currentPreviewItemIndex = 0;
    [parentvc.navigationController presentModalViewController:previewController animated:YES];
    [previewController release];
    UIAlertView* alertView=[[UIAlertView alloc] initWithTitle:@"Alert" message:imageName delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    //[imagecontrller release];


    return NO;
}else {
    return YES;
}

}


Normally, if [UIViewController presentModalViewController:animated:] doesn't work, it is because the existing view controller doesn't have it's parentViewController property set. Basically, it isn't properly hooked into the view hierarchy. There are all sorts of reasons for this, but ... that's what you should look into.

You need to either (a) find another view controller that does have a parentViewController and have that do the presentModal, or (b) carefully re-work your code so that each of the view controllers are aware of their parent, or ...

(c) The easy solution here is to keep a reference to your top-level view controller, and do the presentModal from there. When you're ready to dismiss the modal view controller, it can actually call dismissModalViewControllerAnimated: on itself, and that will get forwarded to the view controller that presented it. Convenient.

0

精彩评论

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

关注公众号