简体   繁体   English

将数组从1个视图控制器发送到2个其他单独的视图控制器

[英]Send array from 1 view controller to 2 other separate view controllers

I have an array in one view controller that I want to send to two other view controllers - i am doing it via a prepareForSegue: 我在一个视图控制器中有一个数组,想要发送给其他两个视图控制器-我正在通过prepareForSegue:

var userId = ""

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let FollowersUsersViewController = segue.destination as! FollowersUsersViewController
    FollowersUsersViewController.followersUserId = userId

    let FollowingUsersViewController = segue.destination as! FollowingUsersViewController
    FollowingUsersViewController.followingUserId = userId
}

For the other 2 view controllers I am calling the array like so: 对于其他2个视图控制器,我这样调用数组:

View controller 1: 视图控制器1:

var followersUserId = String() 
let followersProfileId = followersUserId

View controller 2: 查看控制器2:

var followingUserId = String() 
let followingProfileId = followingUserId

It seems to work when i send it to one of the view controllers (and comment out the other) but then only one of the buttons work when i run the app. 当我将其发送到视图控制器之一(并注释掉另一个)时,它似乎可以工作,但是当我运行该应用程序时,只有一个按钮可以工作。 When I do it for both view controllers (as above) i get a Thread 1: signal SIGABRT error - how can it work for both? 当我对两个视图控制器都执行此操作时(如上所述),我得到了Thread 1: signal SIGABRT错误-两者如何工作?

Appreciate any help :) 感谢任何帮助:)

This is crashing because you are trying to cast wrong view controller. 这是崩溃的,因为您尝试投射错误的视图控制器。 Thats is in a single iteration the segue.destination either FollowersUsersViewController or FollowingUsersViewController . 那是在单次迭代中segue.destination要么FollowersUsersViewControllerFollowingUsersViewController

You have conditionally cast it before use. 您已在使用前有条件地对其进行了铸造。
Try this code 试试这个代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let controller = segue.destination as? FollowersUsersViewController {
        controller.followingUserId = userId
    } else if let controller = segue.destination as? FollowingUsersViewController {
        controller.followingUserId = userId
    }
}

One thing you could do to improve this code is to make both VC's conform to a protocol that enforces this variable's presence: 可以改进此代码的一件事是使两个VC都符合强制该变量存在的协议:


protocol UserIdController {
    var userId: UUID { get set }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewController = segue.destination as? UserIdController {
            viewController.userId = userId
    }
}

It would be less descriptive but much more elegant and swift-like. 它的描述性会降低,但会更加优雅和快捷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当数据源和委托与View Controller分开时,如何将Table View的单击项发送到其他View Controller - How to send clicked item of Table View to other view controller when datasource and delegate are separate from View Controller 如何从单独的视图控制器添加到数组 - How to add to an array from a separate view controller 如何从未连接到其他视图控制器的视图控制器传输回视图控制器? - How do I transfer back to a view controller from a view controller that is not connected to other view controllers? 与控制器分开的视图 - separate view from controller 停止导航控制器影响其他视图控制器 - Stop Navigation Controller from affecting other View Controllers 从另一个视图控制器通知视图控制器 - Notify view controllers from another view controller 快速从单独的视图控制器调用函数 - Calling functions from separate view controllers in swift 使用ios中的自定义委托从另一个视图控制器更改一个视图控制器NSSdictonary的值 - Change value of one view controllers NSSdictonary from other view controller using custom delegate in ios 从其他视图控制器表视图引用数组 - Referencing Array from other View Controller Table View 使用View Controller管理其他两个View Controller - Using a View Controller managing two other View Controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM