gpt4 book ai didi

ios - 如何关闭自己的 View Controller 并在点击按钮时显示另一个 View Controller ?

转载 作者:可可西里 更新时间:2023-11-01 03:28:05 27 4
gpt4 key购买 nike

假设我有 3 个 View Controller ,分别标记为“A”、“B”和“C”。现在,“A”是窗口的 rootViewController,当点击按钮时,它会模态显示“B”。在“B”中,当点击一个按钮时,它应该被“A”解除,然后“A”将立即以模态方式呈现 C。如何做到这一点?这是我希望实现此目标的代码,但我没有成功。

在“A”viewController 中,我声明了一个属性,用于在头文件中保存一个 block ,以便在“B”viewController 被“A”关闭时调用。

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

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

-(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” View Controller 作为窗口的 rootViewController,并且“A”以模态方式呈现“B” View Controller 。所有“A”、“B”和“C”都是 View Controller 。

最佳答案

你可以使用协议(protocol),例如如下所示:-

在您的 B viewController 设置协议(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

在 .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 时,不要使用对象设置委托(delegate)

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

关于ios - 如何关闭自己的 View Controller 并在点击按钮时显示另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23930474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com