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

时间:2014-05-29 09:50:12

标签: ios objective-c block presentviewcontroller

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

At" 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];

}

这是" B"解雇被解雇的方法" A"和" A"应该出现" C"立即

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

}

*请注意,我使用" A"将控制器视为窗口的rootViewController," A"礼物" B"视图控制器模态。 所有" A"," B"和" C"是视图控制器。

4 个答案:

答案 0 :(得分: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
.m类中的

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

现在进入你A_viewController .h class:

#import "Bviewcontroller.h"

@interface A_viewController : UIViewcontroller<BviewControllerDelegate>

.m class

- (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];

}

<强>更新

这是我创建一个类似于:

的演示

enter image description here

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

答案 1 :(得分:2)

你正在拿一个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];
    }];
}

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

答案 2 :(得分:1)

你不能同时解雇B和C。

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

  • 按下按钮&#39; B&#39; ,解雇&#39; B&#39;没有动画并设置一个全局BOOL变量来通知你想要呈现&#39; C&#39;。
  • On - (void)viewDidAppear:(BOOL)动画为&#39; A&#39;

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

答案 3 :(得分:1)

似乎不能在没有短暂显示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
{
    ...
}