如何解除自己的视图控制器,并在button水龙头中呈现另一个视图控制器?

假设我有3个视图控制器,分别标记为“A”,“B”和“C”。 现在,“A”是窗口的rootViewController,当点击一个button时,它会模式地显示“B”。 在“B”中,当一个button被轻敲时,应该被“A”解散,然后“A”将立即以模态方式呈现。如何做到这一点? 这是我的代码,希望实现这个目标,但我没有成功这样做。

在“A”viewController中,我声明了一个属性,在“B”viewController被“A”解除时,在头文件中保存一个块。

@property (nonatomic, copy) void (^presentZapLaunch)(void); 

这是“A”viewController呈现方法呈现“B”

 -(void)presentNextViewCon { CYCGestureZapZapViewController *gestureViewCon = [[CYCGestureZapZapViewController alloc]init]; if (!self.presentZapLaunch) { __weak CYCZapZapViewController *weakRefCon = self; self.presentZapLaunch = ^{ CYCZapZapViewController *preventWeakRefCon = weakRefCon; CYCZapZapLaunchViewController *zapLaunch = [[CYCZapZapLaunchViewController alloc]init]; NSLog(@"Called"); [preventWeakRefCon presentViewController:zapLaunch animated:YES completion:nil]; }; } [self presentViewController:gestureViewCon animated:YES completion:nil]; } 

这是被“A”解雇的“B”解雇方法,“A”应立即出现“C”

 -(void)presentNextViewCon { NSLog(@"Hello"); [self.presentingViewController dismissViewControllerAnimated:self completion:^{[(CYCZapZapViewController *)self.presentingViewController presentZapLaunch];}]; } 

*注意,我使用“A”视图控制器作为窗口的rootViewController,而“A”以模态方式呈现“B”视图控制器。 所有“A”,“B”和“C”都是视图控制器。

你可以使用协议,比如说如下:

在您的B viewController设置协议:

 @class Bviewcontroller; @protocol BviewControllerDelegate <NSObject> - (void)BviewcontrollerDidTapButton: (Bviewcontroller *)controller; @end @interface Bviewcontroller : UIViewcontroller @property (nonatomic, weak) id <BviewControllerDelegate> delegate; - (IBAction)ButtonTap:(id)sender; @end 

在.m类

 - (IBAction)ButtonTap:(id)sender { [self.delegate BviewcontrollerDidTapButton:self]; } 

现在在你A_viewController .h类:

 #import "Bviewcontroller.h" @interface A_viewController : UIViewcontroller<BviewControllerDelegate> 

.m类

 - (void)BviewcontrollerDidTapButton: (Bviewcontroller *)controller { [self dismissViewControllerAnimated:YES completion:^{ // here you can create a code for presetn C viewcontroller }]; } 

重要的是,当您从A_viewController预置Bviewcontroller时,请不要使用对象来设置委托

 -(void)presentNextViewCon { bViewcontroller *gestureViewCon = [[bViewcontroller alloc]init]; gestureViewCon.delegate = self; [self presentViewController:gestureViewCon animated:YES completion:nil]; } 

UPDATE

这里是我创build一个演示,工作如下:

在这里输入图像说明

示例代码链接 http://speedy.sh/2acSC/modelDemo.zip

你正在采取一个button,让它命名为controlButton。 将B和Cbutton与自定义的init方法一起传递。 这意味着你的UIViewController A有controllButton引用。 使用该方法

 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 

在A中设置触发块,就像这样

 [_controllButton addTarget:self action:@selector(controllButtonTapped:)....]; - (void)controllButtonTapped:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ // present you c here [self presentViewController:c animated:YES completion:NULL]; }]; } 

但最好的select是与协调员协调您的出席和解雇行动的“调解员devise模式”。

你不能解雇B和同时提出C.

要执行此任务,您应该执行一些任务。

  • 按下'B'上的button,在没有animation的情况下消除'B',并设置一个全局的BOOLvariables来通知你想呈现'C'。
  • On – (void)viewDidAppear:(BOOL)“A”的animation

    if(bool){[self presentViewController:c animated:YES completion:nil]; }

如果不简单地显示A,似乎不可能从B到C,这看起来不专业。 但是,您可以在A的顶部放置一个黑色的子视图,直到您将animation设为C.

在Swift 3:

 class A : UIViewController { ... func showB() { // Adding the black view before dismissing B does not work; // the view is not displayed. let black = UIView() black.backgroundColor = UIColor.black black.frame = self.view.bounds // assumes A is not zoomed let b = B() self.present(b, animated:true, completion: { self.view.addSubview(black) }) // Note: self.present() will start the animation, // then b.imDone will be set. It is done here for // clarity of what happens next, as if it were all // one function. b.imDone = { b.dismiss(animated:false, completion: { self.present(C(), animated:true, completion: { black?.removeFromSuperview() }) }) } } } class B : UIViewController { var imDone : (() -> Void)? ... func f() { imDone?() } ... } class C : UIViewController { ... }