我想为特定项目设置警报,并让 UILocalNotification
从我的 TableView Controller 中打开特定项目并显示其详细信息。换句话说,当出现通知时,我希望能够点击“显示项目”,而不是显示所有项目的列表,我只想查看该特定项目的详细信息。
为此,我需要存储有关特定项目(标题、索引等)的信息。我该怎么做?将我的项目标题存储在 UILocalNotification.userInfo
中?
最佳答案
添加信息
objective-c
localNotification.userInfo =@{@"title":title,@"index":index};
swift
var userInfo = [String:String]()
userInfo["title"] = title
userInfo["index"] = index
notification.userInfo = userInfo
通知到达时获取信息
objective-c
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@“title = %@”,notification.userInfo[@"title"]);
NSLog(@“index = %@”,notification.userInfo[@"index"]);
}
swift
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
println(notification.userInfo?["title"])
println(notification.userInfo?["index"])
}
*********编辑********
将“标题”和“索引”从通知的 userInfo 传递到 Table View Controller
objective-c
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BookNotification" object:notification.userInfo]
// or update table view data source
}
swift
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
NSNotificationCenter.defaultCenter().postNotificationName("BookNotification", object:notification.userInfo)
// or update table view data source
}
在 Table View Controller
中添加观察者,当通知到达时获取“title”和“index”,然后更新 Table View Controller
datasource
并重新加载表格 View
在 TableView Controller 中
objective-c
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveBookNotification:)
name:@"BookNotification"
object:nil];
- (void) receiveTestNotification:(NSNotification *) notification
{
NSString *title = notification.userInfo[@"title"];
NSString *title = notification.userInfo[@"index"];
// Update data source and reload
}
swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveBookNotification:", name:"BookNotification", object: nil)
func receiveTestNotification(notification: NSNotification) {
let title = notification.userInfo?["title"]
let index = notification.userInfo?["index"]
// Update data source and reload
}
关于ios - UILocalNotification 打开特定项目的详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116410/