0

In the app I'm working on, I have a UIViewController sublcass and a UIView subclass. in the storyboard the view controller contains the UIview. in the uiview I'm drawing something but I need it to know some values that it should be getting from the view controller. So I created a custom protocol in the view controller .h file:

@protocol SSGraphViewControllerProtocol <NSObject>

- (void)numberOfSemesters:(int)number;

@end

@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;

and in the UIView class I confirmed it as having the protocol above and I implemented its method. However. when I pass a number from the view controller, UIView doesn't receive it. Using NSLog, I figured out that UIView isn't entering - (void)numberOfS:(int)number; am I doing anything wrong? How can I fix it? and is there another way that I can send data from the UIViewController class to the UIView controller?

Here is the full code: UIViewController.h

@protocol SSGraphViewControllerProtocol <NSObject>

- (void)numberOfSemesters:(int)number;

@end

@interface SSGraphViewController : UIViewController
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
@end

UIViewController.m

@implementation SSGraphViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.delegate numberOfSemesters:2];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

UIView.h

@interface SSGraph : UIView <SSGraphViewControllerProtocol>
@end

UIView.m

static int numberOfS = 0;

@implementation SSGraph

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    SSGraphViewController *graph = [[SSGraphViewController alloc] init];
    graph.delegate = self;
    return self;
}

- (void) numberOfSemesters:(int)number{NSLog(@"YES");
    numberOfSemesters= number;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}
HusseinB
  • 1,321
  • 1
  • 17
  • 41
  • you sure the class in storyboard for the view is correct? – Bryan Chen Mar 23 '13 at 08:51
  • Please post more of your code so we can help you. where did you implement the custom protocol, did you use view.delegate = self, etc... – Scar Mar 23 '13 at 08:53
  • Yes, the view controller has the custom class as the one i created and it's view also has the custom class i created. Btw once i was working with protocols and delegation and i realised that i can't send delegation from a parent to child in a navigation. is that true? – HusseinB Mar 23 '13 at 08:54
  • *"when i pass a number from the view controller, UIView doesn't receive it"* - what does that mean? You can single-step in the debugger to check what happens when you call the delegate function. – Martin R Mar 23 '13 at 08:59
  • @MartinR in the case above i want to send an integer and save it from the view controller to the view. so the protocol method above has an int as a parameter which i will be passing an int to the receiving side. – HusseinB Mar 23 '13 at 09:20
  • @Scar no i didn't add a view.delegate = self. i wanted to try it but now confused where to replace it since there is no segues. should i create an View object and then set it's delegation to self in the viewDidLoad method of the view controller class? – HusseinB Mar 23 '13 at 09:24
  • try this answer for some insight into how to use delegates: http://stackoverflow.com/questions/14061795/how-to-pass-variable-from-uiviewcontroller-to-delegate-uitableviewcontroller/14368382#14368382 – katzenhut Mar 23 '13 at 09:32
  • @user2176995- add - (void)numberOfSemesters:(int)number; method in UIView.m you can and call this method from viewController – iPatel Mar 23 '13 at 09:33
  • @iPatel it's already there. but i'm doubting if i'm calling the method from the viewController the right way. i'm calling it in the viewDidLoad – HusseinB Mar 23 '13 at 09:49

3 Answers3

0

Read This Article, It is best example with Description

http://css.dzone.com/articles/do-not-publishcreating-your

Also read for create Protocol

Following i describe simple Example for How to create protocol

#DetailViewController.h

#import <UIKit/UIKit.h>

@protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
@end


@interface DetailViewController : MasterViewController

@property (nonatomic, assign) id<MasterDelegate> customDelegate; 

#DetailViewController.m

if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)])
{
          [self.customDelegate getButtonTitile:button.currentTitle];    
}

#MasterViewController.m

create obj of DetailViewController

DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];

and add delegate method in MasterViewController.m for get button title.

#pragma mark -
#pragma mark - Custom Delegate  Method

-(void) getButtonTitile:(NSString *)btnTitle;
{
    NSLog(@"%@", btnTitle);

}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • that's exactly how i did it, but can't delegation work from the master view to the detail view? i mean i can't i create the protocol in the .h file of the masterView and use it the detail view? actually in my case, i have a UIViewController and a UIView and i want to pass data from the view controller to the view.. is there any way possible to do that ? even without delegation? i was thinking about using a singleton . – HusseinB Mar 23 '13 at 09:03
0

You're creating a view controller instance inside of initWithFrame:, assigning its delegate to be self, and then not keeping a reference to the controller or adding its view into the view hierarchy. This is certainly not what you meant to do. Make the connection in your storyboard instead, by making the delegate property an IBOutlet and connecting them by right clicking on the view controller and dragging from the circle next to the property name onto your view instance.

As an aside I'm not convinced of the utility of using a protocol in this way. If the view needs to know some information to do its job, if should either expose some properties that can be set by the controller, or declare a dataSource protocol and query its dataSource rather than rely on the view controller defining the interface it needs.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
0
// Add an observer to your ViewController for some action in uiview
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveActionNotification:)
                                             name:@"someActionNotification"
                                           object:nil];

// Post Notification and method in your Viewcontroller will be called
[[NSNotificationCenter defaultCenter] postNotificationName:@"someActionNotification" object:self];

// at the end Dont forget to remove Observer.
[[NSNotificationCenter defaultCenter] removeObserver:@"someActionNotification"];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57