gpt4 book ai didi

iOS 通过委托(delegate)关闭 View Controller

转载 作者:行者123 更新时间:2023-11-28 22:08:04 25 4
gpt4 key购买 nike

我试图通过委托(delegate)关闭 View Controller ,但它没有响应。

My HomePage 是顶 View Controller ,当屏幕的某个区域被触摸时,Rules View Controller 被推到 HomePage 的顶部。我希望主页 View Controller 通过委托(delegate)关闭规则 View Controller

//主页

@interface HomePage : UIViewController <RulesControllerDelegate>
@end

//首页.m

#import "HomePage.h"
@implementation HomePage

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}


-(void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *)event{
int offset = 30;
UITouch *theTouch = [touches anyObject];
CGPoint location = [theTouch locationInView:self.view];

if (location.x > gameLabel.frame.origin.x - offset && location.y < gameLabel.center.y + offset && location.y > gameLabel.center.y - offset) {
NSLog(@"touched game");
}

if (location.x < rulesLabel.frame.origin.x + rulesLabel.frame.size.width + offset && location.y < rulesLabel.center.y + offset && location.y > rulesLabel.center.y - offset) {
NSLog(@"touched rules");

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Rules"];
[self presentViewController:controller animated:YES completion:nil];
}
}

#pragma mark - RulesControllerDelegate

- (void)rulesControllerDidCancel:(Rules *)controller{

[self dismissViewControllerAnimated:YES completion:nil];
}

@end

//规则.h

@class Rules;
@protocol RulesControllerDelegate <NSObject>
- (void)rulesControllerDidCancel:(Rules *)controller;
@end

@interface Rules : UIViewController

@property (nonatomic, weak) id <RulesControllerDelegate> delegate;

- (IBAction)cancel:(id)sender;

@end

//规则.m

#import "Rules.h"

@implementation Rules
@synthesize delegate;

- (IBAction)cancel:(id)sender {
[delegate rulesControllerDidCancel:self]; //why isn't this called//////////////////////
//[self dismissViewControllerAnimated:YES completion:nil];
}

@end

最佳答案

在您的 touchesBegan 方法中,您需要将 Rules 委托(delegate)设置为 self。

Rules *controller = (Rules *)[self.storyboard instantiateViewControllerWithIdentifier:@"Rules"];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];

关于iOS 通过委托(delegate)关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23502506/

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