Objective c 手动在ViewController之间切换

Objective c 手动在ViewController之间切换,objective-c,Objective C,我在viewcontroller1中,我想进入viewcontroller2,在1中,我要: UIViewController *sv=[[signUpView alloc]init]; [self addChildViewController:sv]; 其中,signUpView是新的viewControllerclass(视图控制器的子类) 什么都没有发生 我试过:[self.view removeFromSuperview] 这确实起了作用,消除了我的观点 如何转换到2 如何在1上加2,

我在
viewcontroller1
中,我想进入
viewcontroller2
,在
1
中,我要:

UIViewController *sv=[[signUpView alloc]init];
[self addChildViewController:sv];
其中,
signUpView
是新的viewController
class
(视图控制器的子类) 什么都没有发生

我试过:
[self.view removeFromSuperview]
这确实起了作用,消除了我的观点

  • 如何转换到2
  • 如何在1上加2,然后返回1

  • 谢谢。

    将您的代码替换为:

    UIViewController *sv=[[signUpView alloc]init];
    [self addChildViewController:sv];
    [self.view addSubview:sv.view];
    [sv didMoveToParentViewController:self];
    
    我没有添加任何注释,因为这段代码是不言自明的。
    这应该会有帮助

    一般方法是:

        [ParentViewController.view addSubview: ChildViewController.view];
        [ParentViewController addChildViewController: ChildViewController];
        [ChildViewController didMoveToParentViewController: ParentViewController];
    
    在您的情况下,它将是:

        [self.view addSubview: sv.view];
        [self addChildViewController: sv];
        [sv didMoveToParentViewController: self];
    

    另请参阅Apple.com上的文章。

    另一种选择是使用segues在故事板中设置
    UIViewController
    交互,然后以编程方式调用segue。如果你不知道如何设置一个segue,那么看看Ray Wenderlich写的或者搜索一个;那里有很多,相比之下,我写的任何东西都会显得苍白

    由于所有内容都在故事板中配置,因此代码很简单。这是我调用
    viewController1
    切换到
    viewController2
    的示例


    [self-PerformsgueWithIdentifier:@“DoNumber2”发送方:self]

    为什么不使用pushviewcontroller或presentviewcontroller。要返回V1,您将获得一个后退按钮(按ViewController)。或使用presentviewcontroller解除IssViewController。