gpt4 book ai didi

ios - 从其 UIViewController 中的按钮关闭 UIPopoverController

转载 作者:行者123 更新时间:2023-11-28 19:06:20 27 4
gpt4 key购买 nike

这是firstPopoverViewController.h代码:

#import <UIKit/UIKit.h>

@interface firstPopoverViewController : UIViewController

@end

这是我的 firstPopoverViewController.m 代码:​​

#import "firstPopoverViewController.h"

@interface firstPopoverViewController ()

@end

@implementation firstPopoverViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.contentSizeForViewInPopover = CGSizeMake(300, 290);

// Header label
UILabel *h1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 85)];
h1.font = [UIFont fontWithName:@"myFont" size:22.0];
h1.textColor = [UIColor blackColor];
h1.textAlignment = NSTextAlignmentCenter;
h1.text = @"Heading";
h1.numberOfLines = 0;
h1.backgroundColor = [UIColor greenColor];

// Ok button BG View
UIView *buttonBG = [[UIView alloc] initWithFrame:CGRectMake(0, 300-75, 300, 75)];
buttonBG.backgroundColor = [UIColor greenColor];

// Ok button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(300/2-130/2, 290-35-15, 130, 35);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"OK" forState:UIControlStateNormal];
[button addTarget:self action:@selector(closePop) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted=YES;

// Adding views
[self.view addSubview:h1];
[self.view addSubview:buttonBG];
[self.view addSubview:button];

}

-(void)closePop {

}

@end

然后是ViewController.h:

#import <UIKit/UIKit.h>
#import "firstPopoverViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) UIButton *popButton;
@property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
@property (strong, nonatomic) UIPopoverController *firstPopover;

@end

最后是 ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

/* ##### UIPopController stuff ##### */
UIImage *popButtonImage = [UIImage imageNamed:@"menu.png"];
_popButton = [UIButton buttonWithType:UIButtonTypeCustom];
_popButton.frame = CGRectMake(0, 0, 73, 66);
[_popButton addTarget:self action:@selector(openPop) forControlEvents:UIControlEventTouchUpInside];
_popButton.adjustsImageWhenHighlighted=NO;
[_popButton setImage:popButtonImage forState:UIControlStateNormal];

[self.view addSubview:_popButton];

}

-(void)openPop {

if (_firstPopoverViewController == nil) {
//Create the _firstPopoverViewController.
_firstPopoverViewController = [[firstPopoverViewController alloc] init];
}

if (_firstPopover == nil) {
_firstPopover = [[UIPopoverController alloc] initWithContentViewController:_firstPopoverViewController];
_firstPopover.popoverContentSize = CGSizeMake(300, 290);
[_firstPopover presentPopoverFromRect:CGRectMake(0,0, 73, 66) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
NSLog(@"show");
} else {
NSLog(@"dismiss");
[_firstPopover dismissPopoverAnimated:YES];
_firstPopover = nil;
}
}

@end

这是显示按钮的非常基本的代码,当我单击此按钮时它会显示弹出窗口。我想使用 firstPopoverViewControll.m 文件中的按钮关闭此弹出窗口。有一个 closePop{} 方法,我应该在里面放什么来关闭这个弹出窗口?谢谢。

顺便说一句,如您所见,我是初学者,我研究了 stackoverflow 并且有一些与委托(delegate)有关的解决方案,这似乎对其他人有用,但对我没有用,你能告诉我一个解决方案吗我发布的代码?非常感谢你们。

最佳答案

可能有一个我不知道的更简单的方法,但下面的方法应该有效:

使用 NSNotificationCenter 将通知发回包含 UIPopOverController 的 ViewController,告诉它关闭弹出窗口。

首先,在ViewController.m viewDidLoad中添加:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closePop:) name:@"ClosePopOver" object:nil];

然后在ViewController.m中添加如下方法:

- (void)closePop:(NSNotification *)notification {
[_firstPopover dismissPopoverAnimated:YES];
}

然后在 irstPopoverViewController.m 中:

- (void)closePop {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ClosePopOver" object:nil];
}

这应该可以解决问题。

关于ios - 从其 UIViewController 中的按钮关闭 UIPopoverController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20126770/

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