【问题标题】:Delegate form UIView to UIViewController将表单 UIView 委托给 UIViewController
【发布时间】:2017-01-17 08:52:33
【问题描述】:

我有 UIView,称为 Popup 和从 UIViewContorller(ParentVC) 弹出 在 UIView 我有 4 个按钮。按下按钮时,需要从(ParentVC)打开新的控制器。我正在使用 Delegate,这是我的错误吗?

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

在 .m 中我有这个:

@synthesize delegate;
....
- (IBAction)stepOfRestoration:(id)sender {
 [self.delegate buttonPressed];
}

这是父.m

    ...
    CGRect rect = CGRectMake(0,0,200,300);
    Popup *popup1 = [[Popup alloc] initWithFrame:rect];
    popup1.delegate = self;
   ....

-(void)buttonPressed {
[self performSegueWithIdentifier:@"infoSegue" sender:nil];
}

那是我的错误吗?

【问题讨论】:

  • if ( [delegate respondsToSelector:@selector(buttonPressed)] ){[delegate buttonPressed]; } 同样检查一下控件是否进入了 if 循环。

标签: ios objective-c uiview uiviewcontroller delegates


【解决方案1】:

您的协议中没有一个名为buttonPressed 的方法,您需要在您的协议中调用一个方法,例如

Popup.m
- (IBAction)buttonPressed:(id)sender {
   [self.delegate stepOfRestoration:sender];
}

Parent.m
- (IBAction)stepOfRestoration:(id)sender {
   // some code
}

Link 到冗长但希望对您有所帮助的教程,祝您好运。

【讨论】:

    【解决方案2】:

    在您的Parent .m 中,您必须符合协议中定义的所有方法。在您的 Parent.m 文件中,buttonPressed 方法不存在于协议中。所以用buttonPressed更新以下方法的名称如下:-

    在声明 PopupDelegate 方法时更新以下代码 Popup.h


    - (IBAction)stepOfRestoration:(id)sender;
    


    -(void)buttonPressed;
    

    【讨论】:

      【解决方案3】:

      你不应该在你的协议中添加 IBAction 方法

      改为添加与每个按钮操作对应的以下方法

      //Popup.h

      @protocol PopupDelegate
      @required
      
      - (Void)stepOfRestoration:(id)sender;
      - (Void)clientCall:(id)sender;
      - (Void)readyTo:(id)sender;
      - (Void)givePhone:(id)sender;
      
      @end
      

      并在相应的按钮动作方法中调用这些协议方法

      例如//弹出.m

      - (IBAction)stepOfRestoration:(id)sender {
       [self.delegate stepOfRestoration:sender];
      }
      

      和 //Parent.m

      -(Void)stepOfRestoration:(id)sender{
      // code here
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多