Its possible to change font of UILabel from other ViewController class

会有一股神秘感。 提交于 2019-12-25 06:21:40

问题


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.

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.

Please help me.

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


回答1:


You can use delegate. In your popover Swift file create such protocol:

protocol PopoverDelegate {
    func didSelectFont(font: UIFont)
}

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)
    }
}

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
}

Now implement delegate method:

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 { ...

Hope it helps!



来源:https://stackoverflow.com/questions/30839152/its-possible-to-change-font-of-uilabel-from-other-viewcontroller-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!