1

我有一个带有 nib 和协议的自定义视图(设置视图)。我想将此视图添加到带有委托的视图控制器上。当我设置它的委托时,我的应用程序崩溃了,我不知道我做错了什么。

我没有用于 ViewController 的 NIB。

我附上了我的代码和 View 的 NIB 的几张图片。

-[UIView setDelegate:]: 无法识别的选择器发送到实例 0xb265e20

。H

@protocol  SettingViewDelegate

@required

-(void)CornerSlider:(id)sender;
-(void)MarginSlider:(id)sender;
-(void)BorderSlider:(id)sender;
-(void)ShadowSlider:(id)sender;
-(void)BorderColor:(id)sender;
-(void)ShadowColor:(id)sender;
-(void)remove:(id)sender;
@end

@interface SettingView : UIView

@property (nonatomic, assign) id  delegate;

-(IBAction)CornerSlider1:(id)sender;
-(IBAction)MarginSlider1:(id)sender;
-(IBAction)BorderSlider1:(id)sender;
-(IBAction)ShadowSlider1:(id)sender;
-(IBAction)BorderColor1:(id)sender;
-(IBAction)ShadowColor1:(id)sender;
-(IBAction)remove1:(id)sender;

@end

.m

@implementation SettingView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed: @"Setting"
                                          owner: self
                                        options: nil] objectAtIndex:0];
    }
    return self;
}

-(IBAction)CornerSlider1:(id)sender
{
    [self.delegate CornerSlider:sender];
}
-(IBAction)MarginSlider1:(id)sender
{
    [self.delegate MarginSlider:sender];
}
-(IBAction)BorderSlider1:(id)sender
{
    [self.delegate BorderSlider:sender];
}
-(IBAction)ShadowSlider1:(id)sender
{
    [self.delegate ShadowSlider:sender];
}
-(IBAction)BorderColor1:(id)sender{
    [self.delegate BorderColor:sender];
}
-(IBAction)ShadowColor1:(id)sender{
    [self.delegate ShadowColor:sender];
}
-(IBAction)remove1:(id)sender{
    [self.delegate remove:sender];
}


@end

视图笔尖

设置委托后,应用程序在此处崩溃。

SettingView *settingView=[[SettingView alloc]init];
[settingView setDelegate:self];
[self.view addSubview:settingView];
4

6 回答 6

3

将@required 替换为@optional

@protocol  SettingViewDelegate

@optional 

-(void)CornerSlider:(id)sender;
-(void)MarginSlider:(id)sender;
-(void)BorderSlider:(id)sender;
-(void)ShadowSlider:(id)sender;
-(void)BorderColor:(id)sender;
-(void)ShadowColor:(id)sender;
-(void)remove:(id)sender;
@end
于 2013-09-03T10:54:20.350 回答
0

查看您是否已将委托的所有方法实现到添加“设置视图”的“self.view”类中。另外,请查看您是否已将“SettingsViewDelegate”实施到您将“设置视图”添加为子视图的类中。

有关更多说明,请查看此https://stackoverflow.com/a/12660523/859001

于 2013-09-03T08:34:12.973 回答
0

你试过了吗

@interface BlaBlaViewController : UIViewController<SettingsViewDelegate>

我在你的代码中看到:

-(void)setDelegate:(id)delegate
{
   self.delegate = delegate;
}

这将导致无限循环。您应该使用合成的 _ivar 来重载设置器(或将其全部放在一起)。

于 2013-09-03T08:36:56.083 回答
0

发现错误。那是在IB。我将自定义类(SettingView)设置为文件所有者的类。现在它的视图的自定义类和文件的所有者是 UIView 的类。

在此处输入图像描述

现在它工作正常。 .

于 2013-09-03T09:29:16.720 回答
0

在 SettingsView 实现中使用以下 setter 方法进行委托。

-(void)setDelegate:(id)adelegate 
{
    _delegate = adelegate;
}
于 2013-09-03T09:54:54.333 回答
0

尝试这个 -

。H

@protocol  SettingViewDelegate

-(void)CornerSlider:(id)sender;
-(void)MarginSlider:(id)sender;
-(void)BorderSlider:(id)sender;
-(void)ShadowSlider:(id)sender;
-(void)BorderColor:(id)sender;
-(void)ShadowColor:(id)sender;
-(void)remove:(id)sender;
@end

@interface SettingView : UIView

@property (nonatomic, assign) id  delegate;

-(IBAction)CornerSlider1:(id)sender;
-(IBAction)MarginSlider1:(id)sender;
-(IBAction)BorderSlider1:(id)sender;
-(IBAction)ShadowSlider1:(id)sender;
-(IBAction)BorderColor1:(id)sender;
-(IBAction)ShadowColor1:(id)sender;
-(IBAction)remove1:(id)sender;

@end

.m

-(IBAction)CornerSlider1:(id)sender
{
    if([self.delegate respondsToSelector:@(CornerSlider:)])
    {
        [self.delegate CornerSlider:sender];
    }
}
-(IBAction)MarginSlider1:(id)sender
{
    if([self.delegate respondsToSelector:@(MarginSlider:)])
    {
        [self.delegate MarginSlider:sender];
    }

}
-(IBAction)BorderSlider1:(id)sender
{
    if([self.delegate respondsToSelector:@(BorderSlider:)])
    {
        [self.delegate BorderSlider:sender];
    }

}
-(IBAction)ShadowSlider1:(id)sender
{
    if([self.delegate respondsToSelector:@(ShadowSlider:)])
    {
        [self.delegate ShadowSlider:sender];
    }

}
-(IBAction)BorderColor1:(id)sender{
    if([self.delegate respondsToSelector:@(BorderColor:)])
    {
        [self.delegate BorderColor:sender];
    }

}
-(IBAction)ShadowColor1:(id)sender{
    if([self.delegate respondsToSelector:@(ShadowColor:)])
    {
        [self.delegate ShadowColor:sender];
    }

}
-(IBAction)remove1:(id)sender{
    if([self.delegate respondsToSelector:@(remove:)])
    {
        [self.delegate remove:sender];
    }

}
于 2015-08-27T12:15:29.340 回答