简体   繁体   English

将表单 UIView 委托给 UIViewController

[英]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.

相关问题 UIView使用代理呈现UIViewController - UIView presenting UIViewController with delegate 在UIView / UIViewController关系中定义委托 - Defining a delegate in a UIView/UIViewController relationship 从UIView委托隐藏UIViewController中的按钮 - Hiding buttons in a UIViewController from a UIView delegate 使用委托从 UIView 子类向 UIViewController 发送数据 - Sending Data to UIViewController From UIView Subclass With Delegate 如何正确实现协议-Swift中UIViewController和UIView之间的委托模式 - How to correctly implement protocol - delegate pattern between UIViewController and UIView in Swift 在UIView子类中声明的委托在UIViewController中不起作用? - Delegate declared in UIView subclass won't work in UIViewController? UIView和UIViewController - UIView and UIViewController 如何在 UIView 子类中通过 UIView 约束 UITableView 并在 UIViewController 中保留数据源和委托方法? - How to constraint UITableView over UIView in UIView subclass and keep dataSource and delegate methods in UIViewController? 如何从子类UIView定义传出事件,以便可以调用UIViewController中的任何委托 - How to define an outgoing event from subclassed UIView so any delegate in UIViewController can be called 在UIViewController中调用委托方法 - Calling delegate method in UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM