Aplicación de iPhone que accede a valores de datos de diferentes clases.

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.

Por favor recomiende.

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

preguntado el 27 de agosto de 11 a las 21:08

2 Respuestas

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.

Respondido 28 ago 11, 02:08

Yes can you please let me know how? i would be really thankful to you. i am stuck with it for 2 days - harpreet Kaur

I was trying to access a from D as A *objecta=[[A alloc]init]nslog( objecta.a); but it shows null - harpreet Kaur

*objecta=[[A alloc]init];nslog( objecta.a); --> this would create an additional instance of the A class, that would probably be empty (or just initialized to its default values). You need to access the original instance instead. To do that, you need to hold a valid reference to that instance, or use delegation (which still requires having a reference), or use notifications (overkill, imho). - magma

Yes how can i hold a valid reference of class A in D. or use delegating. Plz suggest - harpreet Kaur

Please give me simple way to access variables of A in D. i really appriciate it - harpreet Kaur

Can you give more context about what specifically you are trying to achieve? It sounds like you want to pass data between several UIViewControllers. 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.

Respondido 28 ago 11, 09:08

Hello James Thanks for your time.Actually i want to pass data from say MyCoolViewController to AnotherClassController. I have a confusion. Where shall i call [delegate sendAStringToAnotherView:@"this is a string"];? and where and how call myViewControllerPointer.delegate=self;? Hope you reply to this. Sorry about it im beggener. - harpreet Kaur

I didnot accept you answer despite of the fact that i really apprciate it coz i have this confusion n as soon as solved i will accept it. thanks - harpreet Kaur

And the declaration of this protocol is in MyCoolViewController class from where i want to send data? - harpreet Kaur

@harpreet Kaur you call [delegate sendAStringToAnotherView:@"this is a string"]; from the .m file of MyCoolViewController, where you send data from. you call myViewcontrollerPointer.delegate=self in whatever class/view controller you would like to act as the delegate for the first view controller, ie the view controller you want to pass data to. - James

Actually i am new that is why asking so many question hope that will be ok with you. Yes agree firstViewControlle is class to where i want to send data, and MyCoolViewController is class from where i send data. and our protocol is also defined in MyCoolViewController?. and -(void)sendStringToAnoterhView method is also in firstViewController. So there are just 2 classes contains all above code? Please confirm above - harpreet Kaur

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.