它可能从其他ViewController类更改UILabel的字体

我有一个由UILabelUIButton组成的ViewController。 OnClick的UIButton一个popOver目前显示tableViewtableView每个单元格代表不同的字体选项。

我想根据用户从tableViewCellselect的字体来更改UILabel的字体。 我如何实现这一点,因为我的UILabeltableView是在不同的viewController类。

请帮帮我。

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var row = indexPath.row // How to update label from here } 

编辑 :我喜欢这个答案,但不能够理解为其在目标c中的返回从不同的视图更新ViewController标签文本

你可以使用委托。 在你的popover Swift文件中创build这样的协议:

 protocol PopoverDelegate { func didSelectFont(font: UIFont) } 

popover类中创build新创build协议的这种实现:

 class popoverviewcontroller : UITableViewController { var delegate: PopoverDelegate? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var row = indexPath.row // How to update label from here delegate.didSelectFont(youFontHere) } } 

现在在你的主视图控制器中,如果你以编程方式展示你的popover ,你应该把你的popover的delegate属性设置为self 。 如果你正在演示故事板的popover,只是处理segue:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. let destination = segue.destinationViewController as! popoverviewcontroller destination.delegate = self } 

现在实现委托方法:

 func didSelectFont(font: UIFont) { //Update label's font } 

当然,不要忘记添加代表到你的主视图控制器:

 class mainViewController: UIViewController, PopoverDelegate { ... 

希望能帮助到你!