Jongers
Jongers

Reputation: 691

iOS - How to access variable from a class to be used in AppleDelegate.m

I have a class named AddAlarmViewController and declared two variables.

Declare Variable

@interface AddAlarmViewController ()
{
    NSString *soundName;
    NSString *soundName2;
}

I have this method to get the name of the alarm tone from picker view

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    soundName = _alarmNames[row];
    soundName2 = [soundName stringByAppendingString:@".caf"];
}

Now, I want to use soundName2 in another class which is AppleDelegate.m. This is the line of code in AppleDelegate.m that I want to do.

notification.soundName = soundName2;

How do I do this?

Upvotes: 0

Views: 68

Answers (2)

Viraj Padsala
Viraj Padsala

Reputation: 1398

@interface AddAlarmViewController ()
{
    NSString *soundName;
    NSString *soundName2;
}
@property (weak, nonatomic) NSString *soundName;
@property (weak, nonatomic) NSString *soundName2;

Now import AddAlarmViewController.h in your Appdelegate.m file Then Create Object of AddAlarmViewController Through That Object you Can Access Those two Strings.

AddAlarmViewController *restInfoView=[[AddAlarmViewController alloc] initWithNibName:@"AddAlarmViewController" bundle:nil];
notification.soundName = restInfoView.soundName2;

Upvotes: 0

Ares
Ares

Reputation: 5903

On your AddAlarmViewController

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    soundName = _alarmNames[row];
    soundName2 = [soundName stringByAppendingString:@".caf"];

    NSDictionary *payload = [NSDictionary dictionaryWithObject:soundName2 forKey:@"NewSound"]];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NewSoundSelected" object:self withUserInfo:payload];

}

Somewhere on your AppDelegate, probably inside of didFinishLaunchingWithOptions:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingWithNewSound:) name:@"NewSoundSelected" object:nil];

Then add the following method to your AppDelegate

-(void) doSomethingWithNewSound:(NSNotification *) notification {
   NSDictionary *payload = [notification userInfo];

   //Here is what you are looking for
   NSString *sound = [payload objectForKey:@"NewSound"];

}

Upvotes: 1

Related Questions