如何创建没有情节提要的ViewController并将一个设置为另一个的委托? [英] How to create ViewControllers without storyboard and set one as delegate of the other one?

查看:57
本文介绍了如何创建没有情节提要的ViewController并将一个设置为另一个的委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我设置委托的第一个VC:

  class DiscoverVC:UIViewController,SetLocationDelegate {var name ="让loc = LocationVC()覆盖func viewDidLoad(){super.viewDidLoad()self.loc.delegate =自我}func getLocation(loc:String){self.name = locself.tableView.reloadData()}} 

这是第二个视图控制器,我从那里通过委托获取数据:

 协议SetLocationDelegate:类{func getLocation(loc:String)}class LocationVC:UIViewController {弱var委托:SetLocationDelegate?覆盖func viewDidLoad(){super.viewDidLoad()self.delegate?.getLocation(loc:"Sam")}} 

每当我尝试传递数据时,它都不会调用该方法,并且委托也不会被调用.急需帮助.

解决方案

注意:我以前的回答是使用Storyboard.但是由于发问者不想使用情节提要,因此我在不使用情节提要的情况下替换了我的答案.[此答案的灵感来自

然后在AppDelegate.swift上使用以下命令修改didFinishLaunching:

  func应用程序(_应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:任何]?)->布尔{窗口= UIWindow(框架:UIScreen.main.bounds)让discoverVC = DiscoverVC()作为UIViewController让navigationController = UINavigationController(rootViewController:discoverVC)navigationController.navigationBar.isTranslucent = falseself.window?.rootViewController = navigationControllerself.window?.makeKeyAndVisible()返回真} 

DiscoverVC.swift看起来像这样:

 导入UIKit类DiscoverVC:UIViewController,SetLocationDelegate {var name ="//一次实例化LocationVC仅用于测试var notVisted = true覆盖func viewDidLoad(){super.viewDidLoad()self.view.backgroundColor = .yellowloadLocationVCOnlyOnce()}func loadLocationVCOnlyOnce(){//仅访问一个不守卫,否则返回{返回}让locationVC = LocationVC()locationVC.delegate =自我self.navigationController?.pushViewController(locationVC,动画:true)}func getLocation(loc:String){self.name = loc打印(名称)}} 

LocationVC如下所示:

 导入UIKit协议SetLocationDelegate:类{func getLocation(loc:String)}class LocationVC:UIViewController {弱var委托:SetLocationDelegate?覆盖func viewDidLoad(){super.viewDidLoad()self.view.backgroundColor = .cyanself.delegate?.getLocation(loc:"Sam")}} 

启动时,它将自动从DiscoverVC(黄色背景)移动到LocationVC(蓝色背景).

然后,单击顶部的后退"按钮后,您将在控制台中看到"Sam"打印.然后,您的视图返回到DiscoverVC(黄色背景).

Here is the first VC where I am setting delegate:

class DiscoverVC: UIViewController, SetLocationDelegate {
    var name = ""
    let loc = LocationVC()
     override func viewDidLoad() {
        super.viewDidLoad()
        self.loc.delegate = self
     }

      func getLocation(loc: String) {
        self.name = loc
        self.tableView.reloadData()
    }
 }

And here is the second view Controller from where I'm getting the data via delegate:

protocol SetLocationDelegate: class {
    func getLocation(loc: String)
}

class LocationVC: UIViewController {

    weak var delegate: SetLocationDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate?.getLocation(loc: "Sam")
    }
}

Whenever I try to pass data it doesn't call the method and delegate doesn't get call. Help much needed.

解决方案

Note: My previous answer was using Storyboard. But since the questioner didn't want to use storyboard, I replace my answer without using storyboard. [ this answer was inspired by https://stackoverflow.com/a/41095757/3549695 ]

First, delete the Main.storyboard. Then in Project -> Deployment Info -> Main Interface (pick LaunchScreen instead of 'Main')

Then on AppDelegate.swift modify didFinishLaunching with the following:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    let discoverVC = DiscoverVC() as UIViewController
    let navigationController = UINavigationController(rootViewController: discoverVC)
    navigationController.navigationBar.isTranslucent = false
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    return true
}

The DiscoverVC.swift looks like this:

import UIKit

class DiscoverVC: UIViewController, SetLocationDelegate {

    var name = ""

    // to instantiate LocationVC once only for testing
    var notVisted = true

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .yellow 
        loadLocationVCOnlyOnce()
    }

    func loadLocationVCOnlyOnce() {
        // only visit one
        guard notVisted else { return }

        let locationVC = LocationVC()
        locationVC.delegate = self 
        self.navigationController?.pushViewController(locationVC, animated: true)

    }

    func getLocation(loc: String) {
        self.name = loc
        print(name)
    }
}

And the LocationVC looks like this:

import UIKit

protocol SetLocationDelegate: class {
    func getLocation(loc: String)
}

class LocationVC: UIViewController {

    weak var delegate: SetLocationDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .cyan 
        self.delegate?.getLocation(loc: "Sam")


    }

}

When you start it will automatically move from DiscoverVC (yellow background) to LocationVC (cyan background).

Then after you click the 'Back' button on top, you will see the 'Sam' printed in your console. And your view returned to DiscoverVC (yellow background).

这篇关于如何创建没有情节提要的ViewController并将一个设置为另一个的委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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