如何关闭自己的视图控制器并在按钮水龙头中显示另一个视图控制器? [英] How to dismiss own view controller and present another view controller in a button tap?
问题描述
假设我有3个视图控制器标有A,B和C。
现在,A是窗口的rootViewController,当点击按钮时它以模态方式显示B。在B中,当按下一个按钮时,它应该被A解雇,然后A将立即以模态方式呈现C.如何做到这一点?
这是我希望实现这一目标的代码,但我没有成功。
Let's say I have 3 view controller labeled "A","B" and "C". Right now, "A" is the rootViewController of the window and it presents "B" modally when a button is tapped. In "B", when a button is tapped it is supposed to be dismissed by "A" and then "A" will present C modally immediately.How can one do that? Here's my code in hope of achieving this goal but I was unsuccessful in doing so.
在AviewController中,我声明了一个属性来保存一个块在BviewController被A解雇时要调用的头文件。
At "A" viewController, I declared a property to hold a block at the header file to be called when "B" viewController is dismissed by "A".
@property (nonatomic, copy) void (^presentZapLaunch)(void);
这是AviewController呈现B的现有方法
This is "A" viewController present method to present "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
This is "B" dismiss method to dismissed by "A" and "A" should present "C" immediately
-(void)presentNextViewCon
{
NSLog(@"Hello");
[self.presentingViewController dismissViewControllerAnimated:self completion:^{[(CYCZapZapViewController *)self.presentingViewController presentZapLaunch];}];
}
*请注意,我正在使用A视图控制器作为窗口的rootViewController,A以模态方式呈现B视图控制器。
所有A,B和C都是视图控制器。
*Note that I'm using "A" view controller as the rootViewController of window, and "A" presents "B" view controller modally. All "A","B" and "C" are view controllers.
推荐答案
你可以使用协议比方说如下: -
you can do using protocol let say for example as bellow:-
进入你的B viewController设置协议:
In to your B viewController setting Protocol :
@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];
}
现在进入你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时不要使用对象设置委托
IMPORTANT when you preseting Bviewcontroller from A_viewController do not set delegate with object like
-(void)presentNextViewCon
{
bViewcontroller *gestureViewCon = [[bViewcontroller alloc]init];
gestureViewCon.delegate = self;
[self presentViewController:gestureViewCon animated:YES completion:nil];
}
更新
这是我创建一个类似于以下的演示:
Here it is i create a demo that working like:
示例代码链接 http://speedy.sh/2acSC/modelDemo.zip
这篇关于如何关闭自己的视图控制器并在按钮水龙头中显示另一个视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!