When the user pushes the button in ViewController1, I want it to call a function in ViewController2. I think the only thing I'm missing is assigning ViewController2 as its own delegate, but how do you declare self as delegate?
ViewController1
protocol VC1Delegate {
func pushThisButton(_ sender: UIButton)
}
class ViewController1: UIViewController {
var delegate: VC1Delegate!
var theButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
buildButton()
}
func pushButton(_ sender: UIButton) {
delegate.pushThisButton(theButton)
}
func buildButton() {
theButton = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
theButton.backgroundColor = UIColor.black
theButton.addTarget(self, action: #selector(pushButton), for: .touchUpInside)
self.view.addSubview(theButton)
}
}
View Controller 2
class ViewController2: UIViewController, VC1Delegate {
// I'm guessing somewhere here I need to declare myself as the delegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func pushThisButton(_ sender: UIButton) {
print("Damnit, Jack! Push the damn button!")
}
}
When you instantiate VC2, you should assign delegate to it.
For example:
protocol VC1Delegate: class {
func pushThisButton(_ sender: UIButton)
}
class ViewController1: UIViewController {
weak var delegate: VC1Delegate?
...
let vc2 = ViewController2()
self.delegate = vc2 // we are in the VC1 context
self.navigationController.pushViewController(vc2, animated: true)
Related
I want to trigger Navigation controller to some other screen when i press the button in UIView class. How can i do this?
//Code for UIView Class in Which Button Iboutlet is created
import UIKit
protocol ButtonDelegate: class {
func buttonTapped()
}
class SlidesVC: UIView {
var delegate: ButtonDelegate?
#IBAction func onClickFinish(_ sender: UIButton) {
delegate?.buttonTapped()
}
#IBOutlet weak var imgProfile: UIImageView!
}
//ViewController Class code in Which Button Protocol will be entertained
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
var slidesVC = SlidesVC()
override func viewDidLoad() {
super.viewDidLoad()
slidesVC = SlidesVC()
// add as subview, setup constraints etc
slidesVC.delegate = self
}
extension BaseVC: ButtonDelegate {
func buttonTapped() {
self.navigationController?.pushViewController(SettingsVC.settingsVC(),
animated: true)
}
}
A more easy way is to use typealias. You have to write code in 2 places. 1. your viewClass and 2. in your View Controller.
in your SlidesView class add a typealias and define param type if you need otherwise leave it empty.
class SlidesView: UIView {
typealias OnTapInviteContact = () -> Void
var onTapinviteContact: OnTapInviteContact?
#IBAction func buttonWasTapped(_ sender: UIButton) {
if self.onTapinviteContact != nil {
self.onTapinviteContact()
}
}
}
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let slidesView = SlidesView()
slidesView.onTapinviteContact = { () in
// do whatever you want to do on button tap
}
}
You can use the delegate pattern to tell the containing ViewController that the button was pressed and let it handle whatever is needed to do next, The view doesn't really need to know what happens.
A basic example:
protocol ButtonDelegate: class {
func buttonTapped()
}
class SomeView: UIView {
var delegate: ButtonDelegate?
#IBAction func buttonWasTapped(_ sender: UIButton) {
delegate?.buttonTapped()
}
}
class ViewController: UIViewController {
var someView: SomeView
override func viewDidLoad() {
someView = SomeView()
// add as subview, setup constraints etc
someView.delegate = self
}
}
extension ViewController: ButtonDelegate {
func buttonTapped() {
self.showSomeOtherViewController()
// or
let vc = NewViewController()
present(vc, animated: true)
}
}
I want to update the layout of a popover, I've already tried using the setNeedsLayout() and layoutIfNeeded() methods but couldn't find a solution.
I have a simple screen with 4 buttons that call a popover, within the popover there are 2 extra buttons:
Button -> hides button3, and moves button1 and button2 to the left.
reposition -> changes the sourceview of the popover from the button1/2/3/4 (sender) to button4.
Picture of the layout
The problem I have is that I'm not able to update/refresh the screen, and the popover keeps pointing to the former source view position.
Picture of the popover
Somehow I need a method that updates my view and which is stronger than layoutIfNeeded() because if the screen is changed from portrait/landscape the popover changes automatically. Here I changed portrait/landscape/portrait
GitHub of the project. GitHub
The code used is the following:
class ViewController: UIViewController, ButtonDidDisappearDelegate {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
#IBOutlet weak var constrain2: NSLayoutConstraint!
var popVC: PopVC?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonPopover(_ sender: UIButton) {
configureAndPresentPopover(from: sender)
}
func buttonDisappear() {
self.constrain2.constant = 100
self.button3.isHidden = !self.button3.isHidden
if !self.button3.isHidden {
self.constrain2.constant = 10
}
}
func repositionPopover() {
DispatchQueue.main.async {
if let popVC = self.popVC {
self.self.popoverPresentationController(popVC.popoverPresentationController!, willRepositionPopoverTo: self.button4.bounds, in: self.button4)
}
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func configureAndPresentPopover(from sender: UIButton) {
popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC
guard let popVC = popVC else {
return
}
popVC.popOverDelegate = self
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = sender
popOverVC?.sourceRect = sender.bounds
popVC.preferredContentSize = CGSize(width: 300, height: 60)
popOverVC?.permittedArrowDirections = .down
self.present(popVC, animated: true)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: CGRect,
in view: UIView) {
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = rect
}
}
and the popover controller:
protocol ButtonDidDisappearDelegate: class {
func configureAndPresentPopover(from sender: UIButton)
func buttonDisappear()
func repositionPopover()
}
class PopVC: UIViewController {
weak var popOverDelegate: ButtonDidDisappearDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet weak var popoverButton: UIButton!
#IBAction func popoverAction(_ sender: Any) {
popOverDelegate?.buttonDisappear()
DispatchQueue.main.async {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
#IBAction func reposition(_ sender: Any) {
popOverDelegate?.repositionPopover()
}
}
You need to specify the new position of the arrow and ask for the layout of the presentation controller's view, not only the presented view.
Try :
func repositionPopover() {
let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
popOverController?.sourceView = button4
popOverController?.containerView?.setNeedsLayout()
popOverController?.containerView?.layoutIfNeeded()
}
You can try setting the sourceview and sourcerect once again. call this function when the sourcview bounds are changed.
I have several viewcontrollers embedded in a UINavigationController. I would like to customize the appearance of the Navigation Bar title for each viewController. What is the best method where to call setCustomTitleInNavBar. If it is called in viewDidLoad, self is not yet initialized and the app will crash. In ViewWillAppear title is not yet displayed when view is shown to user. Please advise alternative implementation if this is not the correct way to do it.
class CustomMethods {
func setCustomTitleInNavBar(textValue:String, VC:UIViewController) -> UIView {
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
titleLabel.text = textValue
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.textAlignment = NSTextAlignment.center
VC.navigationItem.titleView = titleLabel
return VC.navigationItem.titleView!
}
}
//call method on the current view controller to modify the nav bar title
class someViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
setCustomTitleInNavBar(textValue: "Where to come?", VC: self)
}
}
Here is a way to implement it through protocol :
// Protocol
protocol NavigationBarSetUpProtocol: class {
// Add more param if needed
func setupNavigationBar(with title: String)
}
// Default implemention
extension NavigationBarSetUpProtocol where Self: UIViewController {
// Default implementation
func setupNavigationBar(with title: String) {
// configure you VC navigation item with : self.navigationItem.titleView = ...
}
}
// VC A
class ViewControllerA: UIViewController, NavigationBarSetUpProtocol {
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar(with: "HOME")
}
}
// VC B
class ViewControllerB: UIViewController, NavigationBarSetUpProtocol {
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar(with: "PROFILE")
}
}
You can call
navigationItem.title = "Your title"
in viewDidLoad.
I am following MVP architecture for developing an iOS app.The app is quite simple in which onViewDidLoad() I call a web service which returns me some data and I display that data in a table view.
ViewController:
class A : UIViewController{
var presenter : MyPresenter?
override func viewDidLoad() {
presenter = MyPresenter(delegate:self)
presenter.callWS()
}
}
extension A : Mydelegate{
func onSuccess(){
//this doesnt allow my viewcontroller to deint
tablview.delegate=self
tableview.datasource=self
tableview.reloadData()
}
}
protocol MyDelegate : class{
func onSuccess()
}
class MYPresenter {
weak var delegate : MyDelegate?
init(MyDelegate) {
self.delegate=delegate
}
func callWS(){
delegate.onSuccess()
}
}
This onSucces of MyDelegate does not allow my A viewcontroller to deint
Please let me know what i am doing wrong?
I've slightly modified version of your code and run it in a playground:
import UIKit
import PlaygroundSupport
class A : UITableViewController {
var presenter : MyPresenter?
override func viewDidLoad() {
presenter = MyPresenter(delegate:self)
presenter?.callWS()
let gesture = UITapGestureRecognizer(target: self, action: #selector(dismissOnTap))
view.addGestureRecognizer(gesture)
}
func dismissOnTap() {
dismiss(animated: true)
}
deinit {
print("Bye VC")
}
}
extension A : MyDelegate {
func onSuccess(){
//this doesnt allow my viewcontroller to deint
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
}
protocol MyDelegate : class {
func onSuccess()
}
class MyPresenter {
weak var delegate : MyDelegate?
init(delegate: MyDelegate) {
self.delegate = delegate
}
func callWS() {
delegate?.onSuccess()
}
deinit {
print("Bye")
}
}
class B: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
let gesture = UITapGestureRecognizer(target: self, action: #selector(showOnTap))
view.addGestureRecognizer(gesture)
}
func showOnTap() {
let vc = A(style: .plain)
present(vc, animated: true)
}
}
let b = B()
b.view.frame = CGRect(x: 0, y: 0, width: 400, height: 600)
PlaygroundPage.current.liveView = b.view
And everything is deallocating properly. I guess the retain cycle is somewhere else and it's hard to find based on provided code.
I am a beginner of xcode programming. I am trying to do an action, when i click button from A view, the button of B view will be hidden. I already know i can use button.hidden = true; for self view controller but I don't know how to control button from other view.
Thanks
#IBAction func TestBut(sender: UIButton) {
setting.hidden = false
}
Before, you create a custom view with button, button action and a protocol as:
protocol CustomViewDelegate {
func buttonPressed (sender: AnyObject)
}
class CustomView: UIView {
#IBOutlet weak var button: UIButton!
var delegate: CustomViewDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
class func loadViewFromXib() -> CustomView {
return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as! CustomView
}
#IBAction func buttonPressed(sender: AnyObject) {
self.delegate.buttonPressed(sender)
}
}
In your ViewController.
class ViewController: UIViewController, CustomViewDelegate {
var firstView: CustomView?
var secondView: CustomView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstView = CustomView.loadViewFromXib()
self.secondView = CustomView.loadViewFromXib()
firstView!.frame = CGRectMake(0, 0, 100, 100)
secondView!.frame = CGRectMake(0, 200, 100, 100)
firstView!.delegate = self
secondView!.delegate = self
self.view.addSubview(firstView!)
self.view.addSubview(secondView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func buttonPressed(sender: AnyObject) {
if (sender as! UIButton) == self.firstView!.button {
self.secondView?.button.hidden = true
}else {
self.firstView?.button.hidden = true
}
}
}
the button has to be a property of the view( or view controller).
A call would look like this:
view.button.hidden = true