简体   繁体   English

如何在 UIView 中单击 UIButton 时打开 UIViewController

[英]How to Open a UIViewController on Click of UIButton In UIView

I wants to open a UIViewController on Click of UIButton which is inside a UIView .我想在单击UIView内的 UIButton 时打开一个 UIViewController 。 The UIView has it's own .h & .m file & The action property of button is declared inside the it. UIView有它自己的.h & .m文件 & 按钮的 action 属性在其中声明。

the navigation code is working fine if I am calling it from UIViewController of UIbutton which is like如果我从 UIbutton 的 UIViewController 调用它,导航代码工作正常,就像

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
FilterDemoTableViewController *viewController = (FilterDemoTableViewController*)     [mainStoryboard instantiateViewControllerWithIdentifier:  @"FilterDemoTableViewController_Id"];
[[self navigationController]pushViewController:viewController animated:YES];

but if same code is calling from UIView .m file then it's not working plz help me.但是如果从 UIView .m 文件调用相同的代码,那么它不起作用请帮助我。

And it won't.它不会。 You can't call [self navigationController] inside UIView because it doesn't have one.你不能在UIView调用[self navigationController]因为它没有。

All you have to do is to reference the ViewController that owns your UIView and call a method there.你所要做的就是引用拥有你的UIView的 ViewController 并在那里调用一个方法。

Typically, you can do it with NSNotification - not the best way but definitely works.通常,您可以使用NSNotification来做到这一点——这不是最好的方法,但绝对有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton目标是UIViewController而不是自定义UIView - UIButton target to be UIViewController not custom UIView 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? - How to add UIViewController as target of UIButton action created in programmatically created UIView? 单击UIbutton,然后像从下拉菜单一样从上到下打开新的UIviewController - click on UIbutton then open new UIviewController in top to down like pull down 在UIViewController上执行的UIView上的动态创建UIButton AddTarget - Dynamic Creation UIButton AddTarget on UIView executed on UIViewController 如何从 UITableViewCell 按钮创建 UIView 单击单独的 UIViewController? - How to create a UIView from a UITableViewCell button click on a separate UIViewController? 无法在自定义UIView上单击UIButton - cannot click UIButton on custom UIView 单击UIView中的按钮后,使UIButton从UIViewController中消失 - Make UIButton disappear from UIViewController once a button in UIView is clicked 从包含的UIViewController分配对UIView的UIButton的回调 - Assigning a callback to a UIView's UIButton from the containing UIViewController 在已加载子视图(UIView)的UIViewController中单击按钮 - Click button in a UIViewController that was loaded a subView (UIView) 如何从UIView导航UIViewController - How to Navigate UIViewController from UIView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM