presentModalViewController 无法显示
首先,我是 Objective C 世界的新手,所以我的问题可能听起来很幼稚。甚至方法的结构也可能不太合适,所以请耐心等待。我确信我会在前进的过程中学习。
我有一个容器视图控制器“DeckViewController”,其中包含另一个视图控制器“CardDetails”。 CardDetails 有一个滚动视图,用于一次加载 3 个自定义 UIView(CustomWebView),其中加载 HTML。这些视图中加载的 HTML 包含图像,并且要求一旦用户单击页面中的任何图像,它就会在 QLPreviewController 中打开。我已将 HTML 中的所有图像包装在锚标记中,以确保 shouldStartLoadWithRequest
委托处理该图像的加载。
该处理程序是在 CustomWebView 类中编写的,我尝试使用 CustomWebView 的父视图控制器(即 CardDetails)加载预览控制器。
我跟踪了代码,它生成了正确的值,但模式视图控制器根本没有出现。我可能只是错过了一个小链接或错过了这里的全部要点。非常感谢帮助和指导。
shouldStartLoadWithRequest 委托处理程序的代码片段如下
- (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;
}
- }
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,如果
[UIViewController PresentModalViewController:animated:]
不起作用,这是因为现有视图控制器没有设置其parentViewController
属性。基本上,它没有正确地连接到视图层次结构中。造成这种情况的原因有很多,但是......这就是您应该调查的。您需要(a)找到另一个具有parentViewController的视图控制器并让它执行presentModal,或者(b)仔细地重新编写您的代码,以便每个视图控制器都知道他们的父级,或者...
(c) 这里简单的解决方案是保留对顶级视图控制器的引用,并从那里执行presentModal。当您准备好关闭模态视图控制器时,它实际上可以自行调用
dismissModalViewControllerAnimated:
,并且该操作将转发到呈现它的视图控制器。方便的。Normally, if
[UIViewController presentModalViewController:animated:]
doesn't work, it is because the existing view controller doesn't have it'sparentViewController
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.