2015-09-10 8 views
1

我有安排本地通知第一种方法:的iOS - 访问文本变量选择器查看到通知声音名称使用

- (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方法来调用这是resultString选择器视图的文本值并把它放在notification.soundName。

下面是代码会是什么样子:

notification.soundName = resultString; 
+0

你的问题是什么? – user996142

+0

我想使这个代码工作,notification.soundName = resultString;我需要从pickerView方法中获取变量resultString,并在我的notification.soundName中使用它 – theFool

+1

您不能在调度通知文本后进行更改。你应该改变方法的签名为''' - (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate andSound:(NSString *)sound'''并从''调用它 - (void)pickerView:(UIPickerView * )pickerView didSelectRow:(NSInteger)row'''。 – user996142

回答

1
@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选定声音文件的名称。 [这是有史以来最好的做法。]

+0

谢谢你的回答!我从现在开始会记住这一点。 – theFool