1

I have a custom view (settingview) with nib and protocol. i want to add this view on a viewcontoller with delegate. When i set its delegate my app crashes and i don't know what i have done wrong.

i don't have NIB for ViewController.

i am attaching few images of my code and View's 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

app crashes here after setting its delegate.

SettingView *settingView=[[SettingView alloc]init];
[settingView setDelegate:self];
[self.view addSubview:settingView];
Blind Ninja
  • 1,063
  • 13
  • 28

6 Answers6

3

Replace @required with @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
Nevil Lad
  • 533
  • 6
  • 20
  • i want these as Required. – Blind Ninja Sep 03 '13 at 11:34
  • @HarvantS. Then you must have to define those method in .m file and have to declare in .h file – Nevil Lad Sep 03 '13 at 11:38
  • yes they are defined in .m file. and i don't think protocol methods needs to declare in .h file. are they? b'cos they are already declared in my protocol. – Blind Ninja Sep 03 '13 at 11:50
  • @HarvantS. If you have any doubt please read https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html :) – Nevil Lad Sep 03 '13 at 12:00
  • "By default, all methods declared in a protocol are required methods. This means that any class that conforms to the protocol must implement those methods." According to your link the class only needs to implement protocol methods. – Blind Ninja Sep 03 '13 at 12:38
  • @HarvantS. ok then Enjoy :) – Nevil Lad Sep 04 '13 at 07:25
0

See whether you have implemented all the methods of your delegate into your "self.view" class where you add your "Setting View". Also, see whether you had implemented "SettingsViewDelegate" into the class you add "Setting View" as subview.

For more clarification, look this https://stackoverflow.com/a/12660523/859001

Community
  • 1
  • 1
0

Did you try

@interface BlaBlaViewController : UIViewController<SettingsViewDelegate>

I saw in your code:

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

This would result in an endless loop. You should use the synthesized _ivar for overloading the setter (or leave it out all together).

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
0

Mistake found. It was in IB. I was setting Custom Class (SettingView) to File's Owner's Class. Now Its View's Custom Class and File's Owner is UIView's Class.

enter image description here

and now its working fine. .

Blind Ninja
  • 1,063
  • 13
  • 28
0

Use the following setter method for delegate in the SettingsView implementation.

-(void)setDelegate:(id)adelegate 
{
    _delegate = adelegate;
}
Inoka
  • 664
  • 7
  • 16
0

Try this -

.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];
    }

}
Pooja Patel
  • 626
  • 8
  • 12