将数组从ViewController传递到AppDelegate

时间:2015-04-04 00:49:55

标签: ios objective-c arrays uiviewcontroller appdelegate

我有一个名为playbackRates的浮点值数组,在ViewController中设置。我想在appdelegate中的函数中使用这些值。我如何从我的appdelegate中访问这些值?

ViewController.h

@interface P15ViewController : UIViewController <GuitarStringsViewDelegate>
{
    float playbackRates[6];
}

ViewController.m

- (void)viewDidLoad
{

    self.guitarStringView.delegate = self;
    chordNameLabel.text = chordName;
    self.guitarStringView.chordName=chordName;

    if([chordName isEqualToString:@"A"]){
        playbackRates[0] = 1.0;
        playbackRates[1] = 1.0;
        playbackRates[2] = 2 * pow(2, (2/12));
        playbackRates[3] = 2 * pow(2, (2/12));
        playbackRates[4] = 2 * pow(2, (2/12));
        playbackRates[5] = 1.0;
    }
    else if([chordName isEqualToString:@"B"]){
    //set rates
    }
    else if([chordName isEqualToString:@"C"]){
    //set rates
    }
    [super viewDidLoad];

}

appdelegate.h

#import <UIKit/UIKit.h>
#import <PAEEngine/PAEEngine.h>
@interface P15AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

-(void)play:(int)index velocity:(float)velocity;

@end

appdelegate.m - player.rate需要由play方法中for循环中数组中的每个值设置

#import "P15AppDelegate.h"
#import "P15ViewController.h"

@interface P15AppDelegate ()
@property (nonatomic, strong) PAEAudioHost* host;
@property (nonatomic, strong) NSArray* channelStrips;
@property (nonatomic, strong) NSArray* filemnames;
@property (nonatomic) int nextVoice;
@end

@implementation P15AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.host = [PAEAudioHost audioHostWithNumOutputs:2];
    self.filemnames = @[@"guitar-E1.wav", @"guitar-A2.wav", @"guitar-D3.wav",
                        @"guitar-G3.wav", @"guitar-B3.wav", @"guitar-E4.wav"];
    const int numVoices = 8;

    NSMutableArray* channelStrips = [[NSMutableArray alloc] initWithCapacity:numVoices];

    for (int i = 0; i < numVoices; ++i)
    {
        PAEChannelStrip* channelStrip = [PAEChannelStrip channelStripWithNumChannels:2];
        [channelStrips addObject:channelStrip];
    }

    self.channelStrips = [NSArray arrayWithArray:channelStrips];

    PAEAmplitude* amp = [PAEAmplitude amplitudeWithNumInputs:2];
    amp.input = [PAEMixer mixerWithSources:self.channelStrips];
    amp.level.input = [PAEConstant constantWithValue:1.0 / numVoices];

    self.host.mainMix = amp;

    [self.host start];

    return YES;
}

-(void)play:(int)index velocity:(float)velocity;
{

    if (index >= 0 && index < self.filemnames.count)
    {
        PAEAudioFilePlayer* player = [PAEAudioFilePlayer audioFilePlayerNamed:self.filemnames[index]];
        player.loop = NO;
        player.rate - playbackRates[i]; --> **NEED TO ACCESS ARRAY HERE**

        PAEAmplitude* amp = [PAEAmplitude amplitudeWithNumInputs:player.numChannels];
        amp.input = player;
        amp.level.input = [PAEConstant constantWithValue:velocity];

        PAEChannelStrip* channelStrip = self.channelStrips[self.nextVoice];
        channelStrip.input = amp;

        self.nextVoice++;

        if (self.nextVoice == self.channelStrips.count)
            self.nextVoice = 0;
    }
}

@end

1 个答案:

答案 0 :(得分:0)

首先,您需要为名为 P15ViewController UIViewController 子类中的 playbackRates 数组编写公共访问器。

P15ViewController.h add -

- (float)playbackRateAtIndex:(NSInteger)index;

P15ViewController.m add -

- (float)playbackRateAtIndex:(NSInteger)index {
    return playbackRates[index];
}

您的 appDelegate 需要知道此方法,因此请在 P15AppDelegate.m 中导入 P15ViewController.h ,我看到你这样做了#39已经完成了。

然后假设 viewController appDelegate rootViewController ,那么您可以使用以下代码访问它。

替换 -

player.rate - playbackRates[i]; --> **NEED TO ACCESS ARRAY HERE**

使用 -

player.rate = [(P15ViewController*)self.window.rootViewController playbackRateAtIndex:i];

需要进行类型转换,以便能够调用访问者(更具体地说是获取者)

如果需要更直接的访问方式,那么更改getter或添加另一个getter以返回 playbackRates 数组的类型地址,如下所示:P15ViewController.m -

- (float*)playbackRatesArray {
    return &playbackRates;
}

请记住也要更新头文件!

从P15AppDelegate.m这样访问它,因为C数组只是一个指针 -

float* playbackRates = [(P15ViewController*)self.window.rootViewController playbackRatesArray];
player.rate = playbackRates[i];