Its possible to change font of UILabel from other ViewController class

748 views Asked by At

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

There are 1 answers

0
Nikita Zernov On BEST ANSWER

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!