Reputation: 3
I have 4 classes i.e views in my application. Class A, having variable a and b.
After clicking on button which is on view A of class A it leads to class B, which is table view controller. Then class B leads to class C. then class C leads to class D.
Now i want to access values of a and b of class A into class D. I tried it with NSNotification but not succeeded.
Please suggest.
I tried with NSNotification:
i tried with NSNotification like Class A---
-(IBAction) selectButton:(id) sender{
NSString * a = [NSString stringWithFormat:@"Manjinder singh"];
NSDictionary * dict = [NSDictionary dictionaryWithObject:a forKey:@"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"sendMessage" object:self userInfo:dict];
}
Then Class D----
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendMessage:) name:@"sendMessage" object:nil];
}
return self;
}
-(void)sendMessage:(NSNotification *)notification{
A *dil=[[A alloc] init];
nslog(@"dil.a");
NSLog(@"USERINFO:MyUserInfo (its a dictionary):%@",[[notification userInfo] valueForKey:@"1"]);
}
This is the rendom try but basically i want to show variable a and b of class A into class D.
Update:------------ MyCoolViewController.h// a class where data send from
@protocol MyCoolViewDelegate;
@interface MyCoolViewController : UIViewController {
id <MyCoolViewDelegate> delegate;//
}
@property (nonatomic, assign) id delegate;//
@end
@protocol MyCoolViewDelegate <NSObject>//
-(void)sendAStringToAnotherView:(NSString*)string;
@end
MyCoolViewController.m
-(void)viewDidLoad{
[delegate sendAStringToAnotherView:@"this is a string"];
}
firstViewController.m //a class where data sent
-(void)viewDidLoad{
MyCoolViewController *myViewControllerPointer=[[MyCoolViewController alloc] init];
myViewControllerPointer.delegate = self;//
}
-(void)sendAStringToAnotherView:(NSString*)string
{
//displays the string as console output
NSLog(@"plzzzzzz show data",string);
}
value of string is not passed to this class because it is not showing in NSLog output. UPDATED 2---
MyCoolViewController.m
#import “MyCoolViewController.h”
#import "firstViewController.h"
@implementation MyCoolViewController
@synthesize label1,sttr;
@synthesize delegate;//
-(IBAction) selectButton:(id) sender{
if (curri==nil) {
curri=[[CurrancyViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:curri animated:YES];
}
curri=nil;
//CHECK ThIS [curri release];
}
- (void)viewDidLoad
{
[delegate sendAStringToAnotherView:@"this is a string"];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background1.png"]];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Upvotes: 0
Views: 194
Reputation: 2282
Can you give more context about what specifically you are trying to achieve? It sounds like you want to pass data between several UIViewController
s. Here is how to set up a delegate for one of your view controllers:
#import <UIKit/UIKit.h>
@protocol MyCoolViewControllerDelegate;
@interface MyCoolViewController : UIViewController {
id <MyCoolViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id delegate;
@end
@protocol MyCoolViewControllerDelegate <NSObject>
-(void)sendAStringToAnotherView:(NSString*)string;
@end
Then you will should synthesize the delegate
@synthesize delegate;
and then when you want to pass data to, lets say a parent view, call this function:
[delegate sendAStringToAnotherView:@"this is a string"];
In the other view controller, wherever you instantiated the instance of this UIViewController, you need to set that self as the delegate;
myViewControllerPointer.delegate = self;
and then implement the delegate function in the parent view controller.
-(void)sendAStringToAnotherView:(NSString*)string
{
//displays the string as console output
NSLog(string);
}
The fact that you need communicate between views like this could possibly mean that there is a more efficient means of structuring your app. Can't say for sure without more info.
Try and use this template to add a delegate to your own app.
Upvotes: 2
Reputation: 1684
You could use delegation here
D would become the delegate of A, and when you click the button, A sends a message to D with the variables as arguments and D responds by performing a method.
Upvotes: 2