Passing NSString between classes - iphone

this is something I have always had confusion about
I never can seem to find a good explanation, i understand inheritance, but from what i learned that is between the master class and the sublasses of them...What if i want to pass a NSString to another class thats not a subclass of the other
Heres a example:
class1.h
#interface class1 : UIViewController{
NSString *string
}
#property (nonatomic, retain) NSString *string
#end
class1.m
#implementation class1
#synthesize string;
-(void)viewDidLoad{
string = #"IM A STRING AHHH";
}
Now lets say i want to pass that string with what its equal to to another class
class2.h
#import "class1.h"
#interface class2 : UIViewController{
}
#end
class2.m
#implementation class2
//i want to use the NSString in here, how do i do that?
Thanks,
Jacob

First of all use [string retain]; in class 1.
Then, in class 2, import class1. make object of class 1 say cls1. and you can access it by cls1.string;

Smiriti's answer is right...
what else you can do is..
overrirde the init method and pass your NSString as a parameter and use it.

You can create an instance of class1 and then you can access string simply calling
[instance string];
or
instance.string
If you don't want to create an instance of class1 you may define a method such as
+(NSString*)getString;
and then call it from class2
[class1 getString];

If class 2 is loading from class 1 you can send the value as parameters.
-(id)initwithParameters:(NSString *)parameter
{
if(self == [super init])
{
// access the paramenter and store in yo u avariable
}
return self;
}
In class 1
[[class 2 alloc]initwithParameters: ]

Related

NSString is returning as NULL while Importing from ViewController Class

NSString is returning as NULL while Importing from ViewController Class
were i want to import the string variable from 1st class to second class..but its not importing
please find met code below for your reference
ViewController:
#interface ViewController : UIViewController{
NSString * string;
#property(nonatomic, retain)NSString * string;
#end
#implementation ViewController
#synthesize string;
-(IBAction) login:(id) sender{
string = #"HI";
}
class2:
#import"ViewController.h"
#interface class2 : UIViewController{
ViewController * vc;
NSString * string1;
#property(nonatomic, retain)NSString * string1;
#end
#implementation ViewController
#synthesize string1;
-(IBAction) login:(id) sender{
NSLog(#"%#",vc.string);
#end
}
Where NSLog is returning NULL!!!!
Kindly help me!
Thanks in advance
I am not clear about your code but if you want to send string from class1 to class2 then you can follow below method:
1.Declare property in class2, same as you have declare:
NSString * string1;
#property(nonatomic, retain)NSString * string1;
#synthesize string1;
2.Then in class1 where you are navigating to class2 (from where you are initializing class2) just assign your string1 the required value as follows:
class2Obj.string1 = #"Hi";
and In class2 you can access string1.
#pradeepj its returning Null because your vc instance is also Null, i am not sure how are you getting instance of vc. If you are using a UINavigationController then you can get instance of vc by using viewControllers property of UINavigationController.
The second class doesn't just automatically get a reference to the same instance of class1 where you set the string value. Your class2 needs to have a property that is set possibly from class1 that references the instance of class 1 where you set the string
//in class 1
Class2 *vc = [[Class2 alloc] init];
vc.class1 = self;
[self.navigationcontroller pushViewController:vc animated:YES];
just change:
-(IBAction) login:(id) sender{
string = #"HI";
}
to
-(IBAction) login:(id) sender{
self.string = #"HI";
}
this will retain ur variable so that you may find it elsewhere. Just be sure that you have assigned the vc variable correctly before fetching value of string variable.

Passing multiple strings from one class to another class

I am new to iPhone. I have small doubt. I have three strings in class BiblePlayerViewController and I want to pass those 3 strings to appdelegate from this class. How to do that?
create a property NSDictionary in BiblePlayerViewController and add your three strings to the dictionary,so you can read that dictionary where ever you want
NSDictionary *FileDict = [[NSDictionary alloc] initWithObjectsAndKeys:str1,#"key1",str2,#"key2",str3,#"key3",nil];
Create a static reference of Appdelegate and declare NSStrings as class variables in Appdelegate
Put this is appdelegate
+(Appdelegate*)getAppdelegate{
return self
}
and then in your viewcontroller
do appdelegate.string1 = string1 and so on ..
you can also encapsulate these objects in an Array and pass them to appdelegate .
The idea is to get a static reference of Appdelegate.
Create a variable of type NSString in appdelegate.h
NSString *test;
import appdelegate.h in BiblePlayerViewController.m
Now get a reference to appdelegate class using
Appdelegate *ad; //init with some object
//now access the NSString var u just created
ad.test=#"your string";
I think you can use the shared object of the appdelegate class for the similar cases.
in the appdelegate class declare global object as
#define UIAppDelegate ((MyAppDelegateClass *)[UIApplication sharedApplication].delegate)
By declaring this, from any class that imports your AppDelegate class can use this shared object of AppDelegate class.
Then is you have three property declared in AppDelegate as
#interface MyAppDelegateClass : NSObject <UIApplicationDelegate>
{
NSString *string1;
NSString *string2;
NSString *string3;
}
#property (nonatomic,retain) NSString string1;
#property (nonatomic,retain) NSString string2;
#property (nonatomic,retain) NSString string3;
#end
Then in the AppDelegate Implementation
#implementation MyAppDelegateClass
#synthesize string1;
#synthesize string2;
#synthesize string3;
#end
In the class from where you need to send the strings to AppDelegate use like below
You need to import AppDelegate class first
#import "MyAppDelegateClass.h"
#interface MyCustomSenderClass : UIViewController
#end
And in the implementation
#implementation MyCustomSenderClass
- (void) sendStringsToAppDelegate
{
UIAppDelegate.string1 = myString1;
UIAppDelegate.string2 = myString2;
UIAppDelegate.string3 = myString3;
}
#end
Thus you can directly set a value to the AppDelegate from any class imports your AppDelegate class.
I think this helps you.

Difficulty accessing objects in another view controller

I'm setting a string in a view controller called ViewController and trying to access it somewhere else. This is the code:
ViewController.h
NSString *string;
...
#property (retain) NSString *string;
ViewController.m
#synthesize string;
...
-(void)viewDidLoad {
...
string = #"Test";
}
OtherViewController.m
#import "ViewController.h"
...
-(void)viewDidLoad {
ViewController *vc;
vc = [[ViewController alloc] init];
NSLog(#"String: %#", vc.string);
}
However, the log is showing: String: (null). What am I doing incorrectly? Thanks.
The viewDidLoad of ViewController is only called when the view is loaded. The view is lazily loaded when required e.g. when a call to vc.view is made.
I'm not sure what you are trying to achieve but this certainly seems like a code smell to me.
As #Fscheidl points out you are creating a new instance and not accessing an existing instance so this may add to your problem. I still believe your main issue is that you assume viewDidLoad is being called just by creating the viewController, which is not the case
edit : it doesn't necessarily need to be an NSObject class, if you want to, you could also do this on your viewController class, just be sure to also include
-(id)init
on your header
---- end of edit
if you're trying to make a class that's accessible to another view controller, why not try NSObject instead of view controller (considering you only need to take that string value)
for instance, lets call that viewController class "global" class
so at global.h, you put up
#import <Foundation/Foundation.h>
#interface GlobalVar : NSObject
#property (nonatomic, strong) NSString *myString;
-(id)init;
#end
and then, at global.m you put up
#import "GlobalVar.h"
#implementation GlobalVar
#synthesize myString;
-(id)init
{
self = [super init];
if(self)
{
myString = [[NSString alloc]initWithFormat:#"the String"];
}
return self;
}
#end
after this, everytime you need to access the "myString" object that contained in global class, you could put up
at header :
#import "GlobalVar.h"
...
...
#property (nonatomic, strong) GlobalVar *globalVar;
at implementation file :
#synthesize globalVar;
...
...
self.globalVar = [[GlobalVar alloc]init];
NSString *theString = globalVar.myString;
NSLog(#"content of my string is : %#",theString);
there you go ;)
You do create a new instance of ViewController by calling [[ViewController alloc] init]; This instance hasn't had string even set. You have to access that exact instance of ViewController.
If you create the instance of OtherViewController directly from ViewController, you can add the following to OtherViewController.h:
#import "ViewController.h"
#property (nonatomic, retain) ViewController *previousViewController
When creating the OtherViewController, you can then set:
//alloc and init instance of OtherViewController
myOtherViewController.previousViewController = self;
In your viewDidLoad: method, you can then access your string as follows:
NSLog(#"String: %#", previousViewController.string);

how to pass string value from one class to another

I just want to know that how to pass the string value from one class to another..
Actually i have two classes.In first class i fetch string value from the array and i just want to use this string value into my second class.Now i don't know how to pass value between this two classes.Please give me some idea to do that.Should i use some class method to pass the value.But i don't know how to use this class methods.How i create the class methods to set the values from one class and then get the same value from class methods.
Thanks for help
Class1.h:
#interface Class1 : NSObject
{
NSArray *arr;
}
- (NSString *)myString;
#end
Class1.m:
#implementation Class1
- (NSString *)myString
{
return [arr objectAtIndex:0];
}
#end
Class2.h:
#interface Class2 : NSObject
{
}
- (void)methodThatUsesStringFromClass1:(Class1 *)c1;
#end
Class2.m:
#implementation Class2
- (void)methodThatUsesStringFromClass1:(Class1 *)c1
{
NSLog(#"The string from class 1 is %#", [c1 myString]);
}
#end
The simplest way is to define public #property in class where you want to pass your object, for example, for NSString:
// CustomClassA.h
#interface CustomClassA : NSObject
{
}
#property (nonatomic, retain) NSString *publicString;
#end
// CustomClassA.m
#implementation CustomClassA
#synthesize publicString;
#end
In your sender:
//somewhere defined CustomClassA objectA;
[objectA setPublicString:#"newValue"];
But you should understand what means retain, #synthesize and other. Also it is not your current question.
you can use appDelegate.YourStringVaraible =#"store your string";
and then use this YourStringVaraible in any class by using appDelegate.YourStringVaraible
pass the string parameter by overriding the init method.
Class1.m
#implementation Class1
Class2 *class2 = [[Class2 alloc] initWithString:myString];
...
#end
Class2.h
#interface Class2 : NSObject
{
NSString *string2;
}
#end
Class2.m
-(id) initWithString:(NSString*)str {
self = [super init];
if(self) {
string2 = str;
}
return(self);
}

Problems with getting data from a class

I guess this is quite basic, but I am struggling with getting data (in this case a simple string) from a class.
My class is quite simple; it looks like this:
#interface myBook : NSObject {
NSString *titleName;
NSString *titleColour;
}
And then I have all the #property and #synthesise calls. I import this (#import myBook.h) in my viewController, where I want to add books to my library. So I have this set of variables to control my library:
NSMutableArray *myLibrary;
NSUInteger currentBookNumber;
myBook *currentBook;
and:
#property (nonatomic, retain) NSMutableArray *myLibrary;
#property (nonatomic, assign) NSUInteger currentBookNumber;
#property (nonatomic, readonly) NoteBook *currentBook;
I then set up my library like so:
-(void) setupLibrary {
myBook *newBook = [[myBook alloc] init];
newBook.titleName = #"Test";
newBook.titleColour = #"orange";
[myLibrary addObject:newBook];
[myLibrary addObject:newBook];
[myLibrary addObject:newBook];
currentBookNumber = 0;
}
In order to get data from the current book I have the following method:
-(NmyBook *) currentBook {
myBook *thisBk = [self.myLibrary
objectAtIndex:self.currentBookNumber];
return thisBk;
}
Now my problem is that if I want to get data from myLibrary and call this in my viewDidLoad, I will only get nil for the title... where did I go wrong?
- (void)viewDidLoad {
[self setupLibrary];
NSLog(#"TitleName:%#", currentBook.titleName);
Issues I see:
Number 1:
I hope the currentBook method return type is a typo. I'm seeing a -(NmyBook *)... shouldn't that be a - (myBook *)?
Number 2:
You never initialized the currentBook object. Before you call the NSLog, you should go
currentBook = [self currentBook];
Where [self currentBook] calls the currentBook method and sets the returned object as the currentBook object.
Finally, try not having the same names for variables and methods :)
Hope that helps.
You have to use self.currentBook.titleName. currentBook.titleName won't trigger your accessor.
By the way, your setupLibrary is leaking. You'll want to add [newBook release] after the last [myLibrary addObject:newBook] call.
You had called one property currentBook and one method currentBook :
Normally it works but you should call the method getCurrentBook;
and the right to access to a property in his class is to use :
[self.theProperty method];
Use :
newBook.titleName = [NSString stringWithFormat:#"Test"];
newBook.titleColor = [NSString stringWithFormat:#"Orange"];
After you can call :
NSLog(#"TitleName:%#", self.currentBook.titleName);