我有一个具有UITableView
的应用程序的小部件。每当用户轻按一行时,我都想打开主iOS应用程序,使用UITableView
。导航到UIViewController
。
我知道要打开该应用程序,我会做这样的事情:
let url = URL(string: "ProjectName://")
extensionContext?.open(url!, completionHandler: nil)
和选择行,我会这样做:
tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.middle)
不过,我将如何从今天的小部件中一起完成?
将您的索引转换为字符串,并使用自定义URL方案
附加projectName://indexpath.row as String
最终基于索引
projectName://1ProjectName://2
然后使用附加值重定向到您的主应用
let url = URL(string: "ProjectName://1")
extensionContext?.open(url!, completionHandler: nil)
它将调用您的主要应用程序应用程序委托,您可以通过转换回 int
来获得索引 func application(_ application: UIApplication, open urls: URL, sourceApplication: String?, annotation: Any) -> Bool {
let obj = urls.absoluteString.components(separatedBy: "://")[1]
//Here convert string to object back to int
NotificationCenter.default.post(name:"selectrow", object: obj)
return true
}
在您的 viewController 中观察它并将其转换为 int
in viewDidload
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.navigateToPushnotificationScreen), name: "selectrow", object: nil)
然后添加此方法
func navigateToPushnotificationScreen(notification : NSNotification){
let index = notification.object as! String
//Here convert your string object to Int, and select your tableview
}