UILocalNotification 打开特定项目的详细视图控制器
UILocalNotification to open detail view controller for a specific item
我想为特定项目设置警报并让 UILocalNotification
从我的 table 视图控制器打开特定项目并显示其详细信息。换句话说,当出现通知时,我希望能够点击 "Show item" 而不是显示所有项目的列表,我只想查看该特定项目的详细信息。
为此,我需要存储有关特定项目(标题、索引等)的信息。我该怎么做?将我的项目的标题存储在 UILocalNotification.userInfo
?
中
是的,你可以做到。本地通知的 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"])
}
********编辑********
将 "title" 和 "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
}
In Table View Controller
在通知到达时添加观察者获取 "title" 和 "index" 然后更新 Table View Controller
datasource
并重新加载 tableview
里面Table View 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
}
我想为特定项目设置警报并让 UILocalNotification
从我的 table 视图控制器打开特定项目并显示其详细信息。换句话说,当出现通知时,我希望能够点击 "Show item" 而不是显示所有项目的列表,我只想查看该特定项目的详细信息。
为此,我需要存储有关特定项目(标题、索引等)的信息。我该怎么做?将我的项目的标题存储在 UILocalNotification.userInfo
?
是的,你可以做到。本地通知的 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"])
}
********编辑********
将 "title" 和 "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
}
In Table View Controller
在通知到达时添加观察者获取 "title" 和 "index" 然后更新 Table View Controller
datasource
并重新加载 tableview
里面Table View 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
}