07-18-2023, 08:43 PM
There are various ways by which data can be received by a different class in iOS. For example -
1. Direct initialization after the allocation of another class.
2. Delegation - for passing data back
3. Notification - for broadcasting data to multiple classes at a single time
4. Saving in `NSUserDefaults` - for accessing it later
5. Singleton classes
6. Databases and other storage mechanisms, like [p-list files][1], etc.
But for the simple scenario of passing a value to a different class whose allocation is done in the current class, the most common and preferred method would be the direct setting of values after allocation. This is done as follows:
We can understand it using two controllers - **Controller1 and Controller2**
Suppose in Controller1 class you want to create the Controller2 object and push it with a String value being passed. This can be done as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passValue:@"String"];
[self pushViewController:obj animated:YES];
}
In the implementation of the Controller2 class there will be this function as:
@interface Controller2 : NSObject
@property (nonatomic, strong) NSString* stringPassed;
@end
@implementation Controller2
@synthesize stringPassed = _stringPassed;
- (void) passValue:(NSString *)value {
_stringPassed = value; // Or self.stringPassed = value
}
@end
You can also directly set the properties of the Controller2 class in the similar way as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj setStringPassed:@"String"];
[self pushViewController:obj animated:YES];
}
To pass multiple values, you can use the multiple parameters like:
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passValue:@“String1” andValues:objArray withDate:date];
Or if you need to pass more than three parameters which are related to a common feature, you can store the values in a model class and pass that modelObject to the next class
ModelClass *modelObject = [[ModelClass alloc] init];
modelObject.property1 = _property1;
modelObject.property2 = _property2;
modelObject.property3 = _property3;
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passmodel: modelObject];
So in short, if you want to -
1. set the private variables of the second class initialise the values by calling a custom function and passing the values.
2. setProperties do it by directlyInitialising it using the setter method.
3. pass more that 3-4 values related to each other in some manner, then create a model class and set values to its object and pass the object using any of the above process.
[1]:
1. Direct initialization after the allocation of another class.
2. Delegation - for passing data back
3. Notification - for broadcasting data to multiple classes at a single time
4. Saving in `NSUserDefaults` - for accessing it later
5. Singleton classes
6. Databases and other storage mechanisms, like [p-list files][1], etc.
But for the simple scenario of passing a value to a different class whose allocation is done in the current class, the most common and preferred method would be the direct setting of values after allocation. This is done as follows:
We can understand it using two controllers - **Controller1 and Controller2**
Suppose in Controller1 class you want to create the Controller2 object and push it with a String value being passed. This can be done as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passValue:@"String"];
[self pushViewController:obj animated:YES];
}
In the implementation of the Controller2 class there will be this function as:
@interface Controller2 : NSObject
@property (nonatomic, strong) NSString* stringPassed;
@end
@implementation Controller2
@synthesize stringPassed = _stringPassed;
- (void) passValue:(NSString *)value {
_stringPassed = value; // Or self.stringPassed = value
}
@end
You can also directly set the properties of the Controller2 class in the similar way as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj setStringPassed:@"String"];
[self pushViewController:obj animated:YES];
}
To pass multiple values, you can use the multiple parameters like:
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passValue:@“String1” andValues:objArray withDate:date];
Or if you need to pass more than three parameters which are related to a common feature, you can store the values in a model class and pass that modelObject to the next class
ModelClass *modelObject = [[ModelClass alloc] init];
modelObject.property1 = _property1;
modelObject.property2 = _property2;
modelObject.property3 = _property3;
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passmodel: modelObject];
So in short, if you want to -
1. set the private variables of the second class initialise the values by calling a custom function and passing the values.
2. setProperties do it by directlyInitialising it using the setter method.
3. pass more that 3-4 values related to each other in some manner, then create a model class and set values to its object and pass the object using any of the above process.
[1]:
[To see links please register here]