iOS 如何从 Uiview 呈现 Modal ViewController [英] iOS how present Modal ViewController from Uiview
问题描述
我有一个 UIView
的子类,现在我需要显示一个视图控制器,但 UIView
没有方法来呈现视图控制器.这是我的问题谢谢
i have subclass a UIView
and now i need to show a view controller but UIView
not have method to present view controller.
this is my problem
thank's
这是我的 uiview 子类中的一段代码
this is a piece of code inside my uiview subclass
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tabella isEqualToString:@"Articoli"]) {
NSDictionary *itm=(NSDictionary*)[comanda objectAtIndex:indexPath.row];
Articoli *aboutViewController = [[Articoli alloc] initWithNibName:@"Articoli" bundle:nil];
aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
aboutViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
aboutViewController.idarticolo=[itm objectForKey:@"idarticolo"];
CGRect aboutSheetFrame = aboutViewController.view.frame;
UIViewController *viewController = [UIViewController new];
viewController.view = self;
//here xcode give me a red error
[self presentViewController:viewController animated:YES completion:nil] ;
aboutSheetFrame =CGRectZero;
aboutViewController.view.superview.bounds = aboutSheetFrame;
}
}
推荐答案
当你需要在 UIView
实例和 UIViewController
之间进行通信时,有一些已知的 iOS 概念,你应该遵守的.正如你已经发现 UIView
不能真正呈现一个新的控制器(缺少 presetViewController:animated:completion
方法或 navigationController
属性,它们都是存在于 UIViewController
中).
When you need a communication between UIView
instance and UIViewController
, there are a few known iOS concepts, which you should adhere to. As you have figured out that UIView
cannot really present a new controller (missing either presetViewController:animated:completion
methods or navigationController
property, which are both present in UIViewController
).
视图应该是代码中最可重用的部分,因此您必须想办法将视图设计为完全不知道它们所在的位置.他们通常只知道用户交互.
Views are supposed to be the most reusable parts of your code, so you must think of a way to design your views to be completely blind to where they are at. They usually only know about user interaction.
首先,您必须重构您的视图.
- 如果你的
UIView
应该是一个UIControl
(有某种目标选择器),你需要在你的控制器中使用 add target 从视图交互中获取回调. - 您可以使用
UITableView
或UICollectionView
中使用的委托模式,它被设计为协议. - 您可以使用添加到视图中的手势识别器(例如
UITapGestureRecognizer
),以便控制器了解用户交互.
- If your
UIView
is supposed to be aUIControl
(has some kind of target selectors), you need to use add target in your controller to get callback from view interaction. - You can use delegate pattern as used in
UITableView
orUICollectionView
, which is designed as a protocol. - You can use gesture recognizers added to a view (
UITapGestureRecognizer
for example), so the controller knows about user interaction.
您甚至可以混合搭配这些架构模式.
You can even mix and match those architectural patterns.
但是您应该真正地研究 iOS 编程基础知识,以便更好地理解这一点.
But you should really look into iOS programming basics, to understand this better.
此外,我在您的代码中看到的第一个错误是您创建了一个通用的 UIViewController
,当您真正应该创建它的自定义子类时,在 Storyboard 和UIViewController
的单独子类.
In addition the first error I see in your code is that you create a generic UIViewController
, when you should really be creating custom subclasses of it, defined in Storyboard and separate subclass of UIViewController
.
我看到的第二个错误是您的 UIView
响应了 tableView:didSelectRowAtIndexPath:
方法,这实际上应该永远发生.所有这些代码都必须移回一个 UIViewController
子类.
The second error I see is that your UIView
responds do tableView:didSelectRowAtIndexPath:
method, which should in fact never happen. All this code must be moved back to one UIViewController
subclass.
这篇关于iOS 如何从 Uiview 呈现 Modal ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!