从UIPopover发送代理消息到主UIViewController [英] Send a Delegate message from UIPopover to Main UIViewController

查看:182
本文介绍了从UIPopover发送代理消息到主UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的 UIPopover 中使用一个按钮,在我的主要<$ c中创建一个 UITextView $ c> UIViewController 代码我看起来像这样( PopoverView.h 文件):

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

然后在我的 PopoverView.m file:

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];
}

而且在我的 MainViewController.m

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];
}

我正在使用委托协议来链接Popover和ViewController,但是'卡在我如何获得我的 BOOL 语句链接在$ pop $ c $ - (void)buttonAPressed 在PopoverView和MainViewController所以当我按下Popover中的按钮时,文本视图将显示在主VC中。如何做这个?

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?

推荐答案

MainViewController 中,您创建 PopoverView ,请确保设置其委托属性,否则发送消息到 delegate PopoverView 将不执行任何操作。

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

例如,在 MainViewController .m

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

我不知道为什么你需要 sendDelegateMessages 变量。即使使用该bool,您也必须设置委托属性,以便 PopoverView 具有发送消息的实际对象引用。

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];



另外,委托 assign (或 weak (如果使用ARC))而不是声明$ c>保留(请参阅为什么使用弱指针进行委派?解释):


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;



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


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.

这篇关于从UIPopover发送代理消息到主UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆