可以从其他ViewController类更改UILabel的字体 [英] Its possible to change font of UILabel from other ViewController class
问题描述
我有一个由UILabel
和UIButton
组成的ViewController. OnClick UIButton
和popOver
出现,显示tableView
. tableView
的每个单元格代表不同的字体选项.
I have a ViewController which consist of UILabel
and UIButton
. OnClick UIButton
a popOver
present which show tableView
. each cell of tableView
represent different font option.
我想根据用户从tableViewCell
中选择的字体来更改UILabel
的字体.我如何实现此目标,因为我的UILabel
和tableView
在不同的viewController类中.
I want to change the font of UILabel
based on user selected font from tableViewCell
. how i can achieve this as my UILabel
and tableView
are in different viewController class.
请帮助我.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var row = indexPath.row
// How to update label from here
}
Edit : I fond this answer but not able to understand as its return in objective c update ViewController label text from different view
推荐答案
您可以使用委托.在您的popover
Swift文件中,创建以下协议:
You can use delegate. In your popover
Swift file create such protocol:
protocol PopoverDelegate {
func didSelectFont(font: UIFont)
}
在您的popover
类中,创建新创建的协议的此类实现:
In your popover
class create such implementation of newly created protocol:
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
,则应将弹出窗口的delegate
属性设置为self
.如果您要从情节提要中显示弹出窗口,则只需处理segue:
Now in your main view controller, if you are presenting your popover
programmatically, you should set your popover's delegate
property to self
. If your are presenting popover from storyboard, just handle 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
}
当然不要忘记将委托添加到主视图控制器中:
And of course don't forget to add delegate to your main view controller:
class mainViewController: UIViewController, PopoverDelegate { ...
希望有帮助!
这篇关于可以从其他ViewController类更改UILabel的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!