可以从其他ViewController类更改UILabel的字体 [英] Its possible to change font of UILabel from other ViewController class

查看:159
本文介绍了可以从其他ViewController类更改UILabel的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由UILabelUIButton组成的ViewController. OnClick UIButtonpopOver出现,显示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的字体.我如何实现此目标,因为我的UILabeltableView在不同的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
}

编辑:我喜欢这个答案,但不能理解为它在目标c中的返回

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆