我正在使用 XCode 4.6,并正在尝试在 iOS 上构建一个本地通知功能,该功能将在重新进入应用程序时执行一个功能。基本上我想更改某些标签中的文本并在重新进入应用程序时添加一些声音. 我以为我走在了正确的轨道上,但是当通过本地通知重新进入应用程序时,我的代码只有部分工作。
首先,我将此函数添加到我的 AppDelegate 中:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"test1"); //this traces successfully
myAppViewController * controller = [myAppViewController alloc];
[controller doSomething]; //calling a function in my myAppViewController.m
}
我以为我已经弄明白了,但现在只有 NSLog 在我的 myAppViewController.m 函数中起作用:
-(void)doSomething{
NSLog(@"do something"); //traces successfully
self.notificationTime.text=@"something else"; //nothing happens here, it works in other functions but not here
[self doSomethingElse]; //calling another function from this function for further testing
}
调用下一个函数....
-(void)doSomethingElse{
NSLog(@"do something else"); //this works
//this whole thing doesn't work -- no sound --
NSURL* url = [[NSBundle mainBundle] URLForResource:@"cash" withExtension:@"mp3"];
NSAssert(url, @"URL is valid.");
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.player prepareToPlay];
[self.player play];
//this doesn't work again
self.notificationTime.text=@"something else";
}
我希望在这里得到一些一般性的建议,我将不胜感激。如果有人知道解决问题的完全不同的方法,那也很棒!