iOS-访问选择器视图中的文本变量以在通知声音名称中使用
我有第一种安排本地通知的方法:iOS-访问选择器视图中的文本变量以在通知声音名称中使用,ios,objective-c,localnotification,Ios,Objective C,Localnotification,我有第一种安排本地通知的方法: - (void) scheduleLocalNotificationWithDate:(NSDate *)fireDate { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.category = @"Alarm"; notification.s
- (void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.category = @"Alarm";
notification.soundName = @"Pager.caf";
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
}
然后,我创建了一个选择器视图,用户可以使用此方法在列表中选择报警音:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *resultString = _alarmNames[row];
_alarmToneText.text = resultString;
}
现在,我想编辑scheduleNotification方法来调用picker视图中的文本值,即resultString,并将其放入notification.soundName中
下面是代码的样子:
notification.soundName = resultString;
对于所选的声音文件名,不需要显示TextFied。[这是有史以来最好的做法。]您的问题是什么?我想让这段代码正常工作,notification.soundName=resultString;我需要从pickerView方法获取变量resultString,并在我的notification.SoundName中使用它。在计划通知文本后,您不能更改通知文本。您应该将方法的签名更改为类似于
-(void)scheduleLocalNotificationWithDate:(NSDate*)fireDate和sound:(NSString*)sound
,并从-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)调用它行
。我已经更新了scheduleLocalNotification方法,但如何从pickerView方法调用它?对不起,你是说首先选择声音名称,然后创建本地通知。?对吧?谢谢你的回答!从现在起我会记住这件事的。
@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
}
}