iOS - 在选择器视图中访问文本变量以在通知声音名称中使用

iOS - Accessing text variable in Picker View to use in Notification Sound Name

我有第一种安排本地通知的方法:

- (void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = fireDate;
    notification.category = @"Alarm";
    notification.soundName = @"Pager.caf";
    [[UIApplication sharedApplication] scheduleLocalNotification: notification];
}

然后,我创建了一个 Picker 视图,用户可以使用此方法在列表中选择警报音:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    NSString *resultString = _alarmNames[row];
    _alarmToneText.text = resultString;
}

现在,我想编辑 scheduleNotification 方法以在选择器视图中调用文本值 resultString 并将其放入 notification.soundName.

代码如下所示:

notification.soundName = resultString;
@interface ViewController ()
{
    NSString *soundName;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    soundName = @"";
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    soundName = _alarmNames[row];
}

- (void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
    if (![soundName isEqualToString:@""]) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.fireDate = fireDate;
        notification.category = @"Alarm";
        notification.soundName = soundName;
        [[UIApplication sharedApplication] scheduleLocalNotification: notification];
    } else {
        // Show alert for select sound
    }

}

无需为选定的声音文件名显示 TextFied。 [这是有史以来的最佳做法。]