Reputation: 179
I've a 'viewContoller' VC1 and a container view in it with a table view controller embedded in VC1.
I've a variable called number which is adding up according to the didSelectRowAtIndexPath
and deducting using didDeselectRowAtIndexRowPath
since its a table.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
number += [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
number -= [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
}
This "number" value belongs to the embedded table controller and I need to access it in VC1.
Additionally the "number" variable constantly changes according to it being selected or deselected and VC1 needs to know when this happens so it has the most updated value.
I've been looking for a solution for days, any help would be appreciated.
Upvotes: 0
Views: 319
Reputation: 254
Sharing data between different classes can be done in various ways but the most cleaner way to do is to keep the shared data outside of all classes who are using/modifying this value by following the loosely coupled design. One can think of a Singleton class but that will be kind a overuse if we have only one property (value) which is being shared.In this case we can have a class with one internal static variable and class methods (with + sign) to access it. Here is the code for the same:
.h file--->
#import <Foundation/Foundation.h>
@interface SharedData : NSObject
+(void)resetNumber;
+(void)incrementNumber;
+(void)decrementNumber;
+(int)getNumber;
@end]
.m File --->
#import "SharedData.h"
@implementation SharedData
static int number;
+(void)resetNumber {
number = 0;
}
+(void)incrementNumber {
number++;
}
+(void)decrementNumber {
number--;
}
+(int)getNumber {
return number;
}
@end
Here is the usage of the class Use @class SharedData only to import it before @implementation
[SharedData incrementNumber]
Hope this might help you !!!!!
Upvotes: 0
Reputation: 2567
Simple, do as below,
1) Go to interface builder and set name to your segue of UIContainerView embed (let's say 'abc
'). Later we are gonna access it from VC1(in step 4).
2) Define your number
variable in VC1 header.
3) In table view controller header define a reference to your VC1 like this,
@property(nonatomic, retain) VC1 *VC;
4) In VC1 class, implement prepareForSegue
and do as below,
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * segueName = segue.identifier;
if ([segueName isEqualToString: @"abc"]) {
TableViewController *tblVC = (TableViewController *) [segue destinationViewController];
tblVC.VC = self;
}
}
5) Now in table view controller you can access your number variable like below,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.VC) {
self.VC.number += [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.VC) {
self.VC.number -= [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
}
}
Now you can access number
variable from VC1
since it is defined in VC1
header. Remember this is just one way to achieve your requirement, There are several ways to handle this kind of situations. Such as, if you want you can do other way around too.
Upvotes: 1
Reputation: 2451
If I get your question correctly, you have a tableView inside a VC and you want to be updated when a user didSelect/DidDeselect a row.
An alternative solution will be having a protocol implementation.
@protocol SpecialTableViewFunctionDelegate <NSObject>
@optional
- (void)didSelectSomethingUpdatedNumber:(NSNumber *)number;
- (void)didDeselectSomethingUpdatedNumber:(NSNumber *)number;
@end
This is of course assuming you subclassed tableView.
On your tableView subclass you can add the delegate for example:
@interface tableviewSubclass :UITableView
@property (nonatomic, weak)id<SpecialTableViewFunctionDelegate>delegate;
@end
then on your did select method you can do something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
number += [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
if(_delegate){
if([_delegate respondsToSelector:(@selector(didSelectSomethingUpdatedNumber:)]){
[_delegate didSelectSomethingUpdatedNumber:number];
}
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
number -= [[[self.sports objectAtIndex:indexPath.row] valueForKey:@"sportNumber"] intValue];
if(_delegate){
if([_delegate respondsToSelector:(@selector(didDeselectSomethingUpdatedNumber:)]){
[_delegate didDeselectSomethingUpdatedNumber:number];
}
}
}
}
then on your VC you just have to assign the delegate to self.
tableViewInstance.delegate = self;
Dont forget to include the delegate to the header <SpecialTableViewFunctionDelegate>
This way you'll be notified when there is a select/deselect of cell
Upvotes: 0