【问题标题】:Setting BOOL to "YES" in custom delegate在自定义委托中将 BOOL 设置为“YES”
【发布时间】:2015-11-24 22:34:15
【问题描述】:

我已经创建了一个委托,以便我的两个不同的视图控制器可以通信,但我试图在我的子视图控制器中将 BOOL 设置为 YES。

childViewController.h

@protocol pageTwoViewControllerDelegate;

@interface pageTwoViewController : UIViewController {
UIButton *takePhotoTransition;
}
@property (nonatomic, weak) id<pageTwoViewControllerDelegate> delegate;

@end

@protocol pageTwoViewControllerDelegate <NSObject>

- (BOOL)didPushTakePhoto;

@end

childViewController.m

...
- (IBAction)takePhotoTransition:(id)sender {

id<pageTwoViewControllerDelegate> strongDelegate = self.delegate;

if ([strongDelegate respondsToSelector:@selector(didPushTakePhoto)]) {
    strongDelegate.didPushTakePhoto = YES; // ERROR: No setter method for 'setDidPushTakePhoto:' for assignment property
}
NSLog(@"Button push recieved");
}

当我的按钮被按下时,我怎样才能克服这个错误并将我的 BOOL 设置为 YES?

【问题讨论】:

标签: ios objective-c delegates boolean


【解决方案1】:

协议只是告诉通过协议知道你的类的每个人,属性 anObject 将在那里。协议不是真实的,它们本身没有变量或方法

尝试将您的代码修改为这样,您正在设置一个不存在的变量或属性。

你必须实现新的 Class 而不是 id

你的协议看起来像

@protocol pageTwoViewControllerDelegate <NSObject>
- (void)setdidPushTakePhoto:(BOOL)aBOOL;
- (BOOL)didPushTakePhoto;
@end

你的 class.h 将包含

@property (nonatomic, getter=get_didPushTakePhoto) BOOL didPushTakePhoto;

你的 class.m 将包含实现

-(BOOL)didPushTakePhoto
{
 return _didPushTakePhoto;
}

- (void)setdidPushTakePhoto:(BOOL)aBOOL{
 _didPushTakePhoto=aBool;
}

【讨论】:

    【解决方案2】:

    您对方法和属性感到困惑。

    你的协议“pageTwoViewControllerDelegate”的定义说它应该实现一个名为“didPushTakePhoto”的方法,它返回一个BOOL值。

    你想要做的是完全不同的。您正在尝试设置一个不存在的属性。每当您访问后跟点“。”的东西时,这应该是该对象所属类的属性。您定义的协议不涉及该属性。

    所以在 if 条件中,您应该在您的委托对象上调用方法“didPushTakePhoto”,如下所示。 [strongDelegate performSelector:@selector(didPushTakePhoto)];

    如果您确定您的委托实现确实实现了协议方法,那么由于您已经将 self.delegate 转换为声明为 id 的 strongDelegate,因此您不需要 if 条件。您可以直接调用如下方法。 [strongDelegate didPushTakePhoto];

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您是否已将您的ChildViewController 的代表分配给ParentViewController

      试试这个:

      ParentChildViewController.m

      #import "ChildViewController.h"
      @interface ParentViewController () <ChildDelegate>
      

      ...

      -(IBAction)btnClicked:(id)sender
      {
          ChildViewController *ctrl = [[ChildViewController alloc] init];
          ctrl._delegate = self;
          // do present childViewController or similar action here
      }
      
      - (void)didPushTakePhoto: (BOOL)result{
          NSLog(@"result: %d",result);
      }
      

      ChildViewController.h

      @protocol ChildDelegate
      - (void)didPushTakePhoto: (BOOL)result;
      @end
      
      @interface pageTwoViewController : UIViewController {
          UIButton *takePhotoTransition;
      }
      @property (assign, nonatomic) id _delegate;
      
      @end
      

      ChildViewController.m

      ...
      - (IBAction)takePhotoTransition:(id)sender {
          if ([self._delegate respondsToSelector:@selector(didPushTakePhoto:)]) {
              [self._delegate didPushTakePhoto:YES];
              // do dismiss here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多