How to pass a parameter to a parent view

Passing parameters to parent object in Objective-C

Here’s the translation of the Polish text to English:

**”In applications with multiple views, we often need to display an additional view where the user can enter parameters (or select from a table). We want to use this information in our parent object.

Example: The calculator screen displayed (see the Savings Calculator with Tax app in the App Store). After changing the capitalization or period (i.e., selecting a value from a table on another view), we want to recalculate taking into account the changed values.

We can use the following methods:

  1. We use a subview instead of views and do not create additional classes.
  2. We use Notification Center
  3. We use object delegation and protocols

Let’s discuss each method:

1. Using Subviews:

In this approach, the additional view is not a separate view controller but rather a subview within the parent view. The subview can contain UI elements for user input or selection, and its properties can be accessed and modified directly from the parent view’s code.

Example:

// Parent View (ViewController.m)

@interface ViewController : UIViewController

@property (nonatomic, strong) UIView *parameterView; // Subview for parameters

- (void)updateCalculationsBasedOnParameters;

@end

// Implementation

- (void)viewDidLoad {
    [super viewDidLoad];

    self.parameterView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 300, 200)];
    [self.view addSubview:self.parameterView];

    // Add UI elements to parameterView for user input or selection
}

- (void)updateCalculationsBasedOnParameters {
    // Read parameter values from subview UI elements
    // Perform calculations based on updated parameters
}

Advantages:

  • Simple implementation for straightforward data passing
  • No need for additional view controller classes

Disadvantages:

  • Tight coupling between parent view and subview
  • Limited flexibility for complex parameter interactions

2. Using Notification Center:

The Notification Center provides a mechanism for objects to broadcast events and receive notifications. The child view can post a notification when a parameter changes, and the parent view can register as an observer to receive that notification and update its data accordingly.

Example:

// Parent View (ViewController.m)

@interface ViewController : UIViewController

- (void)parameterChangedNotification:(NSNotification *)notification;

@end

// Child View (ParameterView.m)

@interface ParameterView : UIView

- (void)sendParameterChangedNotification;

@end

// Implementation

// Parent View (ViewController.m)

- (void)viewDidLoad {
    [super viewDidLoad];

    ParameterView *parameterView = [[ParameterView alloc] initWithFrame:CGRectMake(0, 100, 300, 200)];
    [self.view addSubview:parameterView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(parameterChangedNotification:) name:@"ParameterChangedNotification" object:nil];
}

- (void)parameterChangedNotification:(NSNotification *)notification {
    // Read parameter values from notification's userInfo dictionary
    // Update calculations based on updated parameters
}

// Child View (ParameterView.m)

- (void)sendParameterChangedNotification {
    // Read parameter values from view UI elements
    NSDictionary *userInfo = @{@"parameters": /* parameter values */};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ParameterChangedNotification" object:nil userInfo:userInfo];
}

Advantages:

  • Decoupling between parent and child views
  • Flexible for multiple parent views receiving notifications

Disadvantages:

  • Requires additional notification management code
  • May introduce overhead for frequent parameter changes

3. Using Object Delegation and Protocols:

Delegation is a design pattern that allows objects to communicate by defining a protocol and assigning a delegate object. The child view implements the protocol and defines methods to send parameter updates to the parent view. The parent view sets itself as the delegate of the child view and receives the data through the protocol methods.

Example:

// Parent View (ViewController.m)

@interface ViewController : UIViewController <ParameterViewDelegate>

@property (nonatomic, strong) ParameterView *parameterView;

- (void)parameterView:(ParameterView *)parameterView didUpdateParameters:(NSDictionary *)parameters;

@end

// Child View (ParameterView.m)

@protocol ParameterViewDelegate <NSObject>

- (void)parameterView:(ParameterView *)parameterView didUpdateParameters:(NSDictionary *)parameters;

@end

@interface ParameterView : UIView <UITextFieldDelegate>

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

- (void)sendParameterUpdate

The code for the application implementing parameter passing using methods 2 and 3 is included in the sample application attached below.

Passing parameters to parent object in Objective-C