从 UIPopover 发送 Delegate 消息到 Main UIViewController

发布于 2024-12-27 18:48:09 字数 1675 浏览 1 评论 0原文

我正在尝试使用 UIPopover 中的按钮在我的 Main UIViewController 中创建一个 UITextView 我的代码看起来像这样(< code>PopoverView.h 文件):

@protocol PopoverDelegate <NSObject> 

- (void)buttonAPressed;

@end

@interface PopoverView : UIViewController <UITextViewDelegate> {  //<UITextViewDelegate>

    id <PopoverDelegate> delegate;
    BOOL sendDelegateMessages;
}
@property (nonatomic, retain) id delegate;
@property (nonatomic) BOOL sendDelegateMessages;
@end

然后在我的 PopoverView.m 文件中:

- (void)viewDidLoad
{
    [super viewDidLoad];

UIButton * addTB1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addTB1.frame = CGRectMake(0, 0, 100, 50);
[addTB1 setTitle:@"Textbox One" forState:UIControlStateNormal];
[self.view addSubview:addTB1];    // Do any additional setup after loading the view from its nib.
[addTB1 addTarget:self action:@selector(buttonAPressed) 
forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAPressed
{
    NSLog(@"tapped button one");

    if (sendDelegateMessages)
        [delegate buttonAPressed];
}

也在我的 MainViewController.m 中:

- (void)buttonAPressed {

    NSLog(@"Button Pressed");
    UITextView *textfield = [[UITextView alloc] init];
    textfield.frame = CGRectMake(50, 30, 100, 100);
    textfield.backgroundColor = [UIColor blueColor];
    [self.view addSubview:textfield];
}

我正在使用委托协议链接弹出窗口和ViewController 但我坚持如何让我的 BOOL 语句链接 PopoverView 和 MainViewController 中的 -(void)buttonAPressed ,这样当我按下 Popover 中的按钮时主 VC 中出现一个文本视图。我该怎么做呢?

I'm trying to use a Button in my UIPopover to create a UITextView in my Main UIViewController the code I have looks something like this (PopoverView.h file):

@protocol PopoverDelegate <NSObject> 

- (void)buttonAPressed;

@end

@interface PopoverView : UIViewController <UITextViewDelegate> {  //<UITextViewDelegate>

    id <PopoverDelegate> delegate;
    BOOL sendDelegateMessages;
}
@property (nonatomic, retain) id delegate;
@property (nonatomic) BOOL sendDelegateMessages;
@end

Then in my PopoverView.m file:

- (void)viewDidLoad
{
    [super viewDidLoad];

UIButton * addTB1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addTB1.frame = CGRectMake(0, 0, 100, 50);
[addTB1 setTitle:@"Textbox One" forState:UIControlStateNormal];
[self.view addSubview:addTB1];    // Do any additional setup after loading the view from its nib.
[addTB1 addTarget:self action:@selector(buttonAPressed) 
forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAPressed
{
    NSLog(@"tapped button one");

    if (sendDelegateMessages)
        [delegate buttonAPressed];
}

And also in my MainViewController.m :

- (void)buttonAPressed {

    NSLog(@"Button Pressed");
    UITextView *textfield = [[UITextView alloc] init];
    textfield.frame = CGRectMake(50, 30, 100, 100);
    textfield.backgroundColor = [UIColor blueColor];
    [self.view addSubview:textfield];
}

I'm using a delegate protocol to link the popover and the ViewController but I'm stuck on how I get my BOOL statement to link the -(void)buttonAPressed in the PopoverView and MainViewController so that when I press the button in the Popover a textview appears in the Main VC. How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2025-01-03 18:48:09

在创建 PopoverViewMainViewController 中,请务必设置其 delegate 属性,否则会向 delegate 中的 delegate 发送消息。 code>PopoverView 不会执行任何操作。

例如,在 MainViewController.m 中:

PopoverView *pov = [[PopoverView alloc] initWithNibName:nil bundle:nil];
pov.delegate = self;  // <-- must set this
thePopoverController = [[UIPopoverController alloc] initWithContent...

我不确定为什么需要 sendDelegateMessages 变量。即使使用该布尔值,您也必须设置 delegate 属性,以便 PopoverView 具有用于发送消息的实际对象引用。

如果您想确保 delegate 对象已实现您要调用的方法,您可以这样做:

if ([delegate respondsToSelector:@selector(buttonAPressed)])
    [delegate buttonAPressed];

此外,delegate 属性应使用 assign(如果使用 ARC,则使用 weak)声明,而不是 retain(请参阅为什么使用弱指针进行委托? 进行解释):

@property (nonatomic, assign) id<PopoverDelegate> delegate;

另一件事是,如果您不使用 ARC,则需要在 MainViewController 中的 buttonAPressed 方法末尾添加 [textfield release];以避免内存泄漏。

In MainViewController, where you create PopoverView, be sure to set its delegate property otherwise sending messages to delegate in PopoverView will do nothing.

For example, in MainViewController.m:

PopoverView *pov = [[PopoverView alloc] initWithNibName:nil bundle:nil];
pov.delegate = self;  // <-- must set this
thePopoverController = [[UIPopoverController alloc] initWithContent...

I am not sure why you need the sendDelegateMessages variable. Even with that bool, you must set the delegate property so PopoverView has an actual object reference to send the messages to.

If you want to make sure the delegate object has implemented the method you're about to call, you can do this instead:

if ([delegate respondsToSelector:@selector(buttonAPressed)])
    [delegate buttonAPressed];

Also, the delegate property should be declared using assign (or weak if using ARC) instead of retain (see Why use weak pointer for delegation? for an explanation):

@property (nonatomic, assign) id<PopoverDelegate> delegate;

Another thing is if you're not using ARC, you need to add [textfield release]; at the end of the buttonAPressed method in MainViewController to avoid a memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文