Understanding How to Notify a View Controller About Picker View Events Using Delegation Pattern for UIPickerView Notifications in Swift

Understanding the Delegation Pattern and UIPickerView Notifications

As a developer, you’re likely familiar with the concept of delegation, where one object notifies another about specific events or actions. In this article, we’ll delve into how to notify a view controller that a row has been selected in a UIPickerView using the delegation pattern.

Introduction to Delegation

Delegation is a design pattern used to separate concerns and improve code organization. It allows an object to delegate a task or responsibility to another object, which then takes care of it. In the context of UIPickerView notifications, we’ll explore how to notify a view controller about a selected row.

Understanding UIPickerView Delegates

When you create a custom UIPickerView in your app, you need to set up a delegate class that conforms to the UIPickerViewDataSource and UIPickerViewDelegate protocols. The delegate class will receive notifications from the pickerView about various events, such as when a row is selected.

To notify the view controller about a selected row, we’ll use the UIPickerViewDelegate protocol, specifically the pickerView:didSelectRow:inComponent: method.

Implementing the Delegation Pattern

Let’s break down the process of implementing the delegation pattern for UIPickerView notifications:

Step 1: Create a Delegate Class

First, you need to create a delegate class that conforms to the UIPickerViewDelegate protocol. This class will receive notifications from the pickerView about selected rows.

// PickerDelegate.h
#import <UIKit/UIKit.h>

@protocol PickerDelegate <NSObject>
- (void)pickerClass:(PickerClass *)pickerClass didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
@end

@interface PickerClass : NSObject <UIPickerViewDelegate, PickerDelegate>

Step 2: Conform to the Delegation Protocol

Next, you need to conform your delegate class to the PickerDelegate protocol. This will allow it to receive notifications from the pickerView.

// PickerDelegate.m
#import "PickerClass.h"

@interface PickerClass : NSObject <UIPickerViewDelegate, PickerDelegate>

@end

@implementation PickerClass

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // Notify the delegate about the selected row
    [self.delegate pickerClass:self didSelectRow:row inComponent:component];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _delegate = nil;
    }
    return self;
}

@end

Step 3: Set Up the Delegate

Finally, you need to set up your view controller as the delegate of the UIPickerView. You can do this in your ViewController’s viewDidLoad method.

// ViewController.m
#import "ViewController.h"

@interface ViewController : UIViewController <UIPickerViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Create a UIPickerView and set it as the delegate of the view controller
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.delegate = self;
}

// Implement the pickerView:didSelectRow:inComponent: method here

Passing the Selected Row to the View Controller

Now that you have a delegate class set up, you need to pass the selected row to the view controller. You can do this by calling the pickerClass:didSelectRow:inComponent: method on the delegate instance.

// PickerDelegate.m
- (void)pickerClass:(PickerClass *)pickerClass didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // Pass the selected row to the view controller
    [self.delegate didReceiveSelectedRow:row fromPickerClass:pickerClass];
}

// ViewController.h

@protocol ViewControllerDelegate <NSObject>
- (void)didReceiveSelectedRow:(NSInteger)row fromPickerClass:(PickerClass *)pickerClass;
@end

@interface ViewController : UIViewController <UIPickerViewDelegate, PickerDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Create a delegate instance and set it as the view controller's delegate
    self.delegate = self;
}

// Implement the didReceiveSelectedRow:fromPickerClass: method here

Conclusion

In this article, we explored how to notify a view controller that a row has been selected in a UIPickerView using the delegation pattern. We created a delegate class that conforms to the PickerDelegate protocol and notified the view controller about selected rows. Finally, we passed the selected row to the view controller by calling the pickerClass:didSelectRow:inComponent: method on the delegate instance.

By following these steps, you can effectively use the delegation pattern to notify your view controllers about UIPickerView events, improving the overall organization and maintainability of your app.


Last modified on 2025-03-25