设置委托时崩溃

时间:2013-09-03 08:18:32

标签: iphone ios objective-c delegates

我有一个带有笔尖和协议的自定义视图(settingview)。我想在带有委托的viewcontoller上添加此视图。当我设置它的代理我的应用程序崩溃,我不知道我做错了什么。

我没有ViewController的NIB。

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

-[UIView setDelegate:]: unrecognized selector sent to instance 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

Views Nib

应用程序在设置其委托后崩溃。

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

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

查看您是否已将代理的所有方法都实施到“self.view”类中,其中添加了“设置视图”。另外,看看您是否已将“SettingsViewDelegate”实施到您将“设置视图”添加为子视图的类中。

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

答案 2 :(得分:0)

你试过吗

@interface BlaBlaViewController : UIViewController<SettingsViewDelegate>

我在你的代码中看到了:

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

这将导致无限循环。你应该使用合成的_ivar来重载setter(或者将它们全部放在一起)。

答案 3 :(得分:0)

发现错误。这是在IB。 我将Custom Class(SettingView)设置为File的Owner's Class。现在它的View的自定义类和文件的所有者是UIView的类。

enter image description here

现在它的工作正常。

答案 4 :(得分:0)

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

-(void)setDelegate:(id)adelegate 
{
    _delegate = adelegate;
}

答案 5 :(得分: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];
    }

}