-4

I am really new to IOS developments. I know this is a silly question, but I really have no idea about this. I have two classes. lets assume A and B. in class A, I have a variable called x. I need to use this variable x in my class B. how can I do this?. please explain me with code samples

here is my code in login.h

#import <UIKit/UIKit.h>
#import "jsonpaser.h"
@interface LoginView : UIViewController
{
NSString* PERSON_ID;
}


@property (strong,nonatomic) IBOutlet UIScrollView *scroler;
@property (strong, nonatomic) IBOutlet UITextField *userName;
@property (strong, nonatomic) IBOutlet UITextField *passWord;
@property (strong,nonatomic) jsonpaser* jp;
@property(nonatomic,retain) NSString* PERSON_ID;


@end

in my login.m class there is a method.inside that method i set some value to my PERSONID variable

 PERSON_ID = [[responseObject valueForKey:@"d"]objectForKey:@"PersonId"];

here is my tableviewcontroler.h class

#import <UIKit/UIKit.h>
#import "LoginView.h"
@interface TableViewController : UITableViewController <UITableViewDataSource>
{
LoginView* classobj;
NSString* personIDFromLogin;
}


@property (strong,nonatomic) NSArray* googlePlaces; 
@property (strong,nonatomic) NSArray* finishedArray;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property(retain,nonatomic) LoginView* classobj;
@property(retain,nonatomic) NSString* personIDFromLogin;
@end

this is my tableview.m class

- (void)viewDidLoad
{
[super viewDidLoad];

LoginView* login = [[LoginView alloc]init];
NSString* x = login.PERSON_ID;
NSLog(@"X Variable : %@",x);

NSString* webPath = @"GetAppointmentRequests";
NSString* webURL = [NSString stringWithFormat:@"%@%@",BaseURLString,webPath];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:x,@"caregiverPersonId", nil];

NSLog(@"PARAMETERS :%@",personIDFromLogin);


jsonpaser* parser = [[jsonpaser alloc]init];
[parser getWebServiceResponce:webURL :params success:^(NSDictionary *responseObject){



}];

}

EDIT

i have solve my problem with below link... thank you everyone. sorry for my bad english and grammar mistakes. refer this

Community
  • 1
  • 1
Darshana
  • 314
  • 2
  • 4
  • 22

1 Answers1

0

Basically, you have to send the value of the variable when you are opening a new view i.e UITableViewController

On the TAB Button action

[self performSegueWithIdentifier:@"tableViewControllerSegue" sender:sender];

and in prepareForSegue: method just pass the variable value as

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"tableViewControllerSegue"])
    {   TableViewController *tableViewController = [segue destinationViewController];  
        tableViewController.personIDFromLogin = PERSON_ID;
    }
}
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
  • thank you.. are you telling me this is like we do in android ? in there we pass the variable with contex. if this is like this.. can you tell me is it a proper way to handle this kind of situation ? is there any other ways too ?? – Darshana Mar 12 '14 at 07:39
  • sorry for asking, actually i have use a tab view controller in my app. when user press the tab then this table view appear. i did't do any coding there. just uses a segue – Darshana Mar 12 '14 at 07:43
  • then try to use `prepareForSegue:` – Himanshu Joshi Mar 12 '14 at 07:56
  • Edited the answer .. please check – Himanshu Joshi Mar 12 '14 at 08:03
  • thank you for the help.. can you tell me is there any other ways to do this? i mean without passing the variable, can i access is like we use global variables in android ? – Darshana Mar 12 '14 at 08:23
  • You can use delegate methods or a method to pass that variable – Himanshu Joshi Mar 12 '14 at 08:35
  • actually i have solve my problem using this link http://stackoverflow.com/questions/5488135/objective-c-how-to-access-an-instance-variable-from-another-class. can you give me some link about delegates ? – Darshana Mar 12 '14 at 09:25
  • http://www.tutorialspoint.com/ios/ios_delegates.htm http://code.tutsplus.com/tutorials/ios-sdk-custom-delegates--mobile-10848 – Himanshu Joshi Mar 12 '14 at 09:27