从UIView ios推视图控制器
我有一个基于导航的应用程序。在第一个屏幕上点击导航栏上的一个button,我可以按如下方式推送另一个视图控制器:
-(void) buttonClicked:(id)sender { UIViewController* mv = [[SecondViewController alloc] init]; [[self navigationController] pushViewController:mv animated:YES]; }
现在我有一个UIView(单独的.h和.m文件)作为第一个屏幕的一部分。 点击UIView中的一个button,我想推动SecondViewController。 我已经尝试了以下内容:
UIViewController* mv = [[SecondViewController alloc] init]; UIViewController * home=[[FirstViewController alloc]init]; [[home navigationController] pushViewController:mv animated:YES];
它不工作! 请帮助
UIViewController* mv = [[SecondViewController alloc] init]; UIViewController * home=[[FirstViewController alloc]init]; [[home navigationController] pushViewController:mv animated:YES];
这里的问题是home
不是导航堆栈的一部分,所以[home navigationController]
肯定是零。 我不清楚你在这里做什么,但是创build一个视图控制器并不意味着它实际上是视图控制器graphics的一部分。
为什么它会工作? 随机创build视图控制器的视图是不可见的,是不是解决scheme。 您可以在视图中保留对VC的引用,如下所示:
@imlementation ViewController - (id) init { // ... aView = [[CustomView alloc] init]; aView.viewController = self; // ... } @end @interface CustomView @property (assign) ViewController *viewController; @end
或者你可以在运行时search响应者链:
UIResponder *next = [view nextResponder]; while (next) { if ([next isKindOfClass:[ViewController class]]) { break; } next = [next nextResponder]; }
现在“下一个”将包含视图控制器(如果找不到则为零)。
尝试使用相同的导航控制器推视图,这保持相同的ViewControllers堆栈。
UIViewController* mv = [[SecondViewController alloc] init]; [[self navigationController] pushViewController:mv animated:YES]; [mv release];
我现在看到你的问题了! 你需要#import你的FirstViewController,然后@class它。 然后做你的推。
所以:
//.h #import "FirstViewContoller.h" @class FirstViewController; @interface... //.m -(void)return { FirstViewController *firstview = [[FirstViewController alloc]init(withnibname:)]; [firstView.navigationController pushViewController: firstView.navigationController.topViewController animated: TRUE]; }
如果我没有错,你的UIView虽然是在单独的文件,仍然是从UIViewController类添加到屏幕上。
简单地说,从UIView发布一个通知给你的FirstViewController类,你可以访问导航控制器。 然后从那里推入SecondViewController。
你可以使用这个。 它适用于我: –
首先在UIView类中创buildAppDelegate的对象并初始化它。 然后在Appdelegate.h中创buildNavigationcontroller对象:
@property(strong,nonatomic) UINavigationController *navControl;
在你的UIView类实现这个代码,你想推: –
ViewController *objview = [[ViewController alloc]init]; [appDelegate.navControl pushViewController:objview animated:YES];