Getting appdelegate value from viewcontroller
Asked Answered
L

2

12

In Appdelegate.h
I imported the playerviewcontroller in both Appdelegate.h and Appdelegate.m files.

  @class PlayerViewController; 
  @property(nonatomic)float volumeDelegate;        

In Appdelegate.m

  @synthesize volumeDelegate;
 - (void)volumeChanged:(NSNotification *)notification
 {
volumeDelegate =1.0
    PlayerViewController *volumeObject=[[PlayerViewController alloc]init];
[volumeObject setVolumeForAVAudioPlayer];
}

In Playerviewcontroller.h

 -(void)setVolumeForAVAudioPlayer;

In Playerviewcontroller.m

@interface PlayerViewController ()
{
AppDelegate *appdelegate;

 }
  -(void)viewDidLoad
  {
  appdelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  }

  -(void)setVolumeForAVAudioPlayer
  {
[appdelegate.sharedplayer  setVolume:appdelegate.volumeDelegate];
NSLog(@"System Volume in player view:  %f",appdelegate.volumeDelegate);
 }

When i run this I get volumeDelegate value as Zero as below.

System Volume in player view:0.000000000

What is the Mistake I'm making here

Luxemburg answered 23/8, 2013 at 11:32 Comment(0)
J
35

You can access to the AppDelegate object like below

Objective-C

Define it like this:

AppDelegate appDelegate;

Access it like this:

appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

Usage:

- (void)setVolumeForAVAudioPlayer
{
    [appDelegate.sharedplayer  setVolume:appdelegate.volumeDelegate];
    NSLog(@"System Volume in player view:  %f",appDelegate.volumeDelegate);
}

Swift:

Define it like this:

let appDelegate = UIApplication.shared.delegate as! AppDelegate

Usage:

func setVolumeForAVAudioPlayer() 
{
    appDelegate.sharedPlayer.setVolume:appDelegate.VolumeDelegate
    print("System Volume in player view: \(appDelegate.volumeDelegate)")
}
Jefferyjeffie answered 23/8, 2013 at 11:34 Comment(0)
S
4

You're initializing the appdelegate member variable in a viewDidLoad, but that method is not called at the moment you call setVolumeForAVAudioPlayer!

You're doing

PlayerViewController *volumeObject=[[PlayerViewController alloc]init];
[volumeObject setVolumeForAVAudioPlayer];

But alloc init doesn't make viewController's view to load! viewDidLoad is not called at all.

Static answered 23/8, 2013 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.