【发布时间】:2015-04-04 00:49:55
【问题描述】:
我有一个浮点值数组,称为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
【问题讨论】:
-
您可以将您的数组作为属性保存在应用程序委托中,然后在任何地方使用它。但这也不是一个好主意。您应该考虑创建数据库或外部文件(plist)或 NSUserDefault 之类的东西。
标签: ios objective-c arrays uiviewcontroller appdelegate