[英]Delegate form UIView to UIViewController
I have UIView, what called Popup and poping from UIViewContorller(ParentVC) On UIView I have 4 buttons.我有 UIView,什么叫做 Popup 和从 UIViewContorller(ParentVC) 弹出 在 UIView 我有 4 个按钮。 When buttons is pressed, it needs to open new Controllers from(ParentVC).
当按钮被按下时,它需要从(ParentVC)打开新的控制器。 I am using Delegate, were is my mistake?
我正在使用委托,是我的错误吗?
//Popup.h
@protocol PopupDelegate
@required
- (IBAction)stepOfRestoration:(id)sender;
- (IBAction)clientCall:(id)sender;
- (IBAction)readyTo:(id)sender;
- (IBAction)givePhone:(id)sender;
@end
@interface Popup : PSCustomViewFromXib
@property (nonatomic, assign) id <PopupDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIView *view;
- (IBAction)stepOfRestoration:(id)sender;
- (IBAction)clientCall:(id)sender;
- (IBAction)readyTo:(id)sender;
- (IBAction)givePhone:(id)sender;
In .mi have this:在 .mi 中有这个:
@synthesize delegate;
....
- (IBAction)stepOfRestoration:(id)sender {
[self.delegate buttonPressed];
}
And this is Parent .m这是 Parent .m
...
CGRect rect = CGRectMake(0,0,200,300);
Popup *popup1 = [[Popup alloc] initWithFrame:rect];
popup1.delegate = self;
....
-(void)buttonPressed {
[self performSegueWithIdentifier:@"infoSegue" sender:nil];
}
So were is my mistake?那么是我的错误吗?
You don't have a method called buttonPressed
in your protocol, you need to call a method in your protocol, eg您的协议中没有名为
buttonPressed
的方法,您需要在您的协议中调用一个方法,例如
Popup.m
- (IBAction)buttonPressed:(id)sender {
[self.delegate stepOfRestoration:sender];
}
Parent.m
- (IBAction)stepOfRestoration:(id)sender {
// some code
}
Link to long winded but hopefully helpful tutorial, good luck. 链接到冗长但希望有用的教程,祝你好运。
In your Parent .m
, you must conform all the methods which are defined in protocol.在您的
Parent .m
,您必须符合协议中定义的所有方法。 In your Parent.m file, buttonPressed
method is not present in protocol.在您的 Parent.m 文件中,协议中不存在
buttonPressed
方法。 So update the name of below method with buttonPressed
as follow:-因此,使用
buttonPressed
更新以下方法的名称,如下所示:-
Update below code at Popup.h
while declaring PopupDelegate methods在声明 PopupDelegate 方法的同时更新
Popup.h
以下代码
- (IBAction)stepOfRestoration:(id)sender;
With和
-(void)buttonPressed;
you shouldn't add IBAction methods in your protocol你不应该在你的协议中添加 IBAction 方法
instead add following methods corresponding each button action而是添加以下对应于每个按钮动作的方法
//Popup.h //弹出窗口.h
@protocol PopupDelegate
@required
- (Void)stepOfRestoration:(id)sender;
- (Void)clientCall:(id)sender;
- (Void)readyTo:(id)sender;
- (Void)givePhone:(id)sender;
@end
and call these protocol methods in corresponding button action methods并在相应的按钮动作方法中调用这些协议方法
eg //Popup.m例如//Popup.m
- (IBAction)stepOfRestoration:(id)sender {
[self.delegate stepOfRestoration:sender];
}
and //Parent.m和 //Parent.m
-(Void)stepOfRestoration:(id)sender{
// code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.