gpt4 book ai didi

iOS - 如何从要在 AppleDelegate.m 中使用的类访问变量

转载 作者:行者123 更新时间:2023-11-29 01:39:59 24 4
gpt4 key购买 nike

我有一个名为 AddAlarmViewController 的类并声明了两个变量。

声明变量

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

我有这个方法可以从选择器 View 中获取警报音的名称

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

现在,我想在另一个类 AppleDelegate.m 中使用 soundName2。这是 AppleDelegate.m 中我想要执行的代码行。

notification.soundName = soundName2;

我该怎么做?

最佳答案

在您的 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];

}

在你的 AppDelegate 的某个地方,可能在 didFinishLaunchingWithOptions 里面:

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

然后将以下方法添加到您的 AppDelegate

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

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

}

关于iOS - 如何从要在 AppleDelegate.m 中使用的类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32514046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com