0

我的应用程序有一个今天的小部件,它有一个UITableView. 每当用户点击一行时,我想打开主 iOS 应用程序,UIViewController使用UITableView. 导航到,然后选择对应的单元格。

我知道打开应用程序,我会做这样的事情:

let url = URL(string: "ProjectName://")
extensionContext?.open(url!, completionHandler: nil)

并选择行,我会这样做:

tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.middle)

不过,我将如何从 Today Widget 一起完成这一切?

4

1 回答 1

1

将您的索引转换为字符串并附加您的自定义 url 方案

ProjectName://indexPath.row 作为字符串

最后基于索引

项目名称://1 项目名称://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
        }

在您的视图控制器中观察它并将其转换为Int

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
    }
于 2017-09-04T12:27:04.103 回答