Ios 在Swift3中不带故事板的xib文件之间传递数据

Ios 在Swift3中不带故事板的xib文件之间传递数据,ios,swift3,xib,Ios,Swift3,Xib,我不熟悉swift(也不熟悉StackOverflow),现在正在学习使用nib文件,因为我已经用故事板学习了很多。 在故事板中,我们可以实例化segues和unwindSegues,从一个VC到另一个VC获取数据。如果不使用故事板,我如何做同样的事情 我目前有一个scrollview(MainViewController.xib),它在上面加载一个tableview(ContactView.xib)。我有一个“添加”按钮覆盖在当前没有响应的tableview上。 我希望在单击add按钮向tab

我不熟悉swift(也不熟悉StackOverflow),现在正在学习使用nib文件,因为我已经用故事板学习了很多。 在故事板中,我们可以实例化segues和unwindSegues,从一个VC到另一个VC获取数据。如果不使用故事板,我如何做同样的事情

我目前有一个scrollview(MainViewController.xib),它在上面加载一个tableview(ContactView.xib)。我有一个“添加”按钮覆盖在当前没有响应的tableview上。 我希望在单击add按钮向tableview添加新数据,然后展开以反映表中的新数据时,能够加载另一个视图(AddContact.xib),但不使用情节提要

我可以提供我的代码,如果需要的话,但我只想找人给我指出正确的方向。我知道这可以通过使用导航控制器来实现,但我似乎找不到适合它的教程(大多数都很旧,使用Obj-C。我不熟悉Obj-C)

我甚至研究了一些类似的问题,比如:和 但是他们没有回答我的问题

感谢您的帮助。多谢各位

我的AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var mainVC: MainViewController? = nil


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    mainVC = MainViewController(nibName: "MainViewController", bundle: nil)
    let frame = UIScreen.main.bounds
    window = UIWindow(frame: frame)
    window!.rootViewController = mainVC
    window!.makeKeyAndVisible()
    return true

}
MainViewController.swift:

class MainViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let contactView: ContactView = ContactView(nibName: "ContactView", bundle: nil)
    let dialerView: DialerView = DialerView(nibName: "DialerView", bundle: nil)

    self.addChildViewController(dialerView)
    self.scrollView.addSubview(dialerView.view)
    dialerView.didMove(toParentViewController: self)

    self.addChildViewController(contactView)
    self.scrollView.addSubview(contactView.view)
    contactView.didMove(toParentViewController: self)

    var contactViewFrame : CGRect = contactView.view.frame
    contactViewFrame.origin.x = self.view.frame.width
    contactView.view.frame = contactViewFrame

    self.scrollView.contentSize = CGSize(width: self.view.frame.width * 2, height: self.view.frame.height)
}
}
My ContactView.xib:

class ContactView: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var searchBar: UISearchBar!
var names = ["Rohan", "Rahul", "Sneh", "Paa", "Maa", "Vatsal", "Manmohan"]
var numbers = ["9830000001", "9830000002", "9830000003", "9830000004", "9830000005", "9830000006", "9830000007"]
let navVC: UINavigationController? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    tableView.rowHeight = CGFloat(60)
    tableView.register(UINib(nibName: "ContactCell", bundle: nil), forCellReuseIdentifier: "Contact")
    let cell = tableView.dequeueReusableCell(withIdentifier: "Contact", for: indexPath) as! ContactCell
    cell.nameLabel.text = names[indexPath.row]
    cell.numLabel.text = numbers[indexPath.row]
    return cell
}

// Make delete-able in scroll view
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        names.remove(at: indexPath.row)
        numbers.remove(at: indexPath.row)
        tableView.reloadData()
    }
}
My AddContact.xib仅定义了出口

编辑:多亏了评论,我找到了一个解决方案。 对于任何想知道我是如何做到这一点的人,我将发布以下步骤:

步骤1:创建协议(我在MainViewController.xib中创建了协议)

步骤2:在senderVC中为其实例化一个委托。AddContact.xib是我的senderVC

class AddContact: UIViewController {

var delegate: NewContactDelegate? = nil
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var numField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func saveContact(_ sender: UIButton) {
    if delegate != nil {
        if let name = nameField?.text, let num = numField?.text {
            delegate?.add_Contact(name: name, num: num)
            dismiss(animated: true, completion: nil)
        }
    }
}
步骤3:使ReceivingVC符合委托并接受数据

class ContactView: ....., NewContactDelegate {

func add_Contact(name: String, num: String) {
    names.append(name)
    numbers.append(num)
    addressBook.reloadData() // This is my TableView outlet
}
}
**

注意:记住在ReceivingVC中将委托断言为非nil值,以确保SenderVC中的
@IBAction
按预期工作


**

请提供代码,顺便说一句,您也可以将控制器嵌入导航中,而不仅仅是使用推送和弹出。您需要创建一个
协议
将协议分配给
MainViewController
,并在
AddContact
@Shabirjan上创建该协议的代表。您好,我现在已经提供了代码。我还没有为导航添加任何代码,因为我不知道如何操作。@rudydy对不起,但我不太明白。您的意思是导航控制器的协议吗?
协议
s用于在不同的ViewController之间共享数据。在
youtube
上查找
ios斯坦福讲座
。你会找到详细的讲座,让你开始学习IOS。请提供代码,顺便说一句,您也可以将控制器嵌入导航中,而不仅仅是使用推送和弹出。您需要创建一个
协议
将协议分配给
MainViewController
,并在
AddContact
@Shabirjan Hi上创建该协议的代理。我现在已经提供了代码。我还没有为导航添加任何代码,因为我不知道如何操作。@rudydy对不起,但我不太明白。您的意思是导航控制器的协议吗?
协议
s用于在不同的ViewController之间共享数据。在
youtube
上查找
ios斯坦福讲座
。您将找到详细的讲座,帮助您开始使用iOS
class ContactView: ....., NewContactDelegate {

func add_Contact(name: String, num: String) {
    names.append(name)
    numbers.append(num)
    addressBook.reloadData() // This is my TableView outlet
}
}