首页 文章

如何关闭自己的视图控制器并在按钮水龙头中显示另一个视图控制器?

提问于
浏览
7

假设我有3个视图控制器标记为“A”,“B”和“C” . 现在,“A”是窗口的rootViewController,当点击按钮时它以模态方式呈现“B” . 在“B”中,当按下一个按钮时,它应该被“A”解雇,然后“A”将立即以模态方式呈现C.如何做到这一点?这是我希望实现这一目标的代码,但我没有成功 .

在“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”都是视图控制器 .

4 回答

  • 9

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

    进入你的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
    

    in .m class

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

    Now in to you A_viewController .h class:

    #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 
    
        }];
    }
    

    IMPORTANT 当您从A_viewController预先设置Bviewcontroller时,请不要使用对象设置委托

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

    UPDATE

    这是我创建一个像以下一样工作的演示:

    enter image description here

    SAMPLE CODE LINK http://speedy.sh/2acSC/modelDemo.zip

  • 1

    你正在拿一个Button让它命名为controlButton . 使用自定义init方法将该按钮与B和C一起传递 . 这意味着你的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];
        }];
    }
    

    但最好的选择是采用“调解员设计模式”,协调员正在协调您的现在和解雇行动 .

  • 1

    你不能同时解雇B和C .

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

    • 在'B'上按下按钮,在没有动画的情况下解除'B'并设置全局BOOL变量以通知您要显示'C' .

    • 开 - (void)viewDidAppear:(BOOL)动画'A'

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

  • 2

    似乎不能在没有简单显示A的情况下从B到C,这看起来不专业 . 但是,您可以将黑色子视图放在A顶部,直到您将其设置为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
    {
        ...
    }
    

相关问题