从一个UIView IOS推视图控制器(push a view controller from a

2019-10-16 14:02发布

我在第一个屏幕导航栏上的按钮的基于导航application.On点击,我能够如下推另一个视图控制器:

 -(void) buttonClicked:(id)sender
{

    UIViewController*  mv = [[SecondViewController alloc] init];

    [[self navigationController] pushViewController:mv animated:YES];  

}

现在我有一个UIView(单独h和.m文件)作为第一屏幕的一部分。 在UIView的点击按钮,我想推SecondViewController。 我曾尝试以下:

 UIViewController*  mv = [[SecondViewController alloc] init];
 UIViewController * home=[[FirstViewController alloc]init];
 [[home navigationController] pushViewController:mv animated:YES];

它不工作! 好心帮

Answer 1:

UIViewController*  mv = [[SecondViewController alloc] init];
UIViewController * home=[[FirstViewController alloc]init];
[[home navigationController] pushViewController:mv animated:YES];

这里的问题是, home是不是导航堆栈的一部分,所以[home navigationController]肯定是零。 我不是你想在这里做什么,而只是创建一个视图控制器并不意味着它实际上是视图控制器图的一部分很清楚。



Answer 2:

为什么它的工作? 随机创建视图控制器,其观点是不可见的,甚至,是解决不了问题。 你可以保持这样的观点给VC一个参考:

@imlementation ViewController

- (id) init
{
    // ...
    aView = [[CustomView alloc] init];
    aView.viewController = self;
    // ...
}

@end

@interface CustomView

@property (assign) ViewController *viewController;

@end

或者,你可以搜索在运行时响应链:

UIResponder *next = [view nextResponder];
while (next)
{
    if ([next isKindOfClass:[ViewController class]])
    {
        break;
    }
    next = [next nextResponder];
}

和现在的“下一个”将包含视图控制器(或零,如果它无法找到)。



Answer 3:

尝试使用相同的navigationController推来看,这保持ViewControllers的相同的堆栈。

UIViewController*  mv = [[SecondViewController alloc] init];
[[self navigationController] pushViewController:mv animated:YES]; 

[mv release];


Answer 4:

我现在看到您的问题! 您需要#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];
}


Answer 5:

如果我没看错,你的UIView虽然是在不同的文件中,仍然添加到从UIViewController类的屏幕。

简单地说,从后UIView的通知发送到您FirstViewController类,你可以访问导航控制器。 然后从那里推SecondViewController。



Answer 6:

您可以使用此。 它非常适用于我: -

在UIView类首先创建的AppDelegate的对象,并对其进行初始化。 然后创建Appdelegate.h Navigationcontroller对象: -

@property(strong,nonatomic) UINavigationController *navControl;

在你的UIView类实现此代码要推: -

ViewController *objview = [[ViewController alloc]init]; [appDelegate.navControl pushViewController:objview animated:YES];


文章来源: push a view controller from a UIView ios