user1468157
user1468157

Reputation: 55

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?

Upvotes: 0

Views: 341

Answers (4)

Mathew Varghese
Mathew Varghese

Reputation: 4527

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.

Upvotes: 0

Srinivas Kattimani
Srinivas Kattimani

Reputation: 336

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";

Upvotes: 0

Kunal Balani
Kunal Balani

Reputation: 4789

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.

Upvotes: 0

Bad Boy
Bad Boy

Reputation: 628

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];

Upvotes: 1

Related Questions