从 UIPopover 发送 Delegate 消息到 Main UIViewController
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在创建
PopoverView
的MainViewController
中,请务必设置其delegate
属性,否则会向delegate
中的delegate
发送消息。 code>PopoverView 不会执行任何操作。例如,在
MainViewController.m
中:我不确定为什么需要
sendDelegateMessages
变量。即使使用该布尔值,您也必须设置delegate
属性,以便PopoverView
具有用于发送消息的实际对象引用。如果您想确保
delegate
对象已实现您要调用的方法,您可以这样做:此外,
delegate
属性应使用assign
(如果使用 ARC,则使用weak
)声明,而不是retain
(请参阅为什么使用弱指针进行委托? 进行解释):另一件事是,如果您不使用 ARC,则需要在
MainViewController
中的buttonAPressed
方法末尾添加[textfield release];
以避免内存泄漏。In
MainViewController
, where you createPopoverView
, be sure to set itsdelegate
property otherwise sending messages todelegate
inPopoverView
will do nothing.For example, in
MainViewController.m
:I am not sure why you need the
sendDelegateMessages
variable. Even with that bool, you must set thedelegate
property soPopoverView
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:Also, the
delegate
property should be declared usingassign
(orweak
if using ARC) instead ofretain
(see Why use weak pointer for delegation? for an explanation):Another thing is if you're not using ARC, you need to add
[textfield release];
at the end of thebuttonAPressed
method inMainViewController
to avoid a memory leak.