Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何通过自定义字段查找自定义视图_Ios_Swift_Uiview_Uiviewcontroller - Fatal编程技术网

Ios 如何通过自定义字段查找自定义视图

Ios 如何通过自定义字段查找自定义视图,ios,swift,uiview,uiviewcontroller,Ios,Swift,Uiview,Uiviewcontroller,我有一个视图控制器,可以在其中添加多个自定义视图。我试图做的是访问我动态添加的视图,并在某些触发器(如位置更新)上访问它 像下面这样的。。。我把代码简化了一点。但这应该会让你对我正在努力实现的目标有一个相当好的看法 class MapViewController: UIViewController, UIScrollViewDelegate, UITextFieldDelegate, CLLocationManagerDelegate { //my custom views

我有一个视图控制器,可以在其中添加多个自定义视图。我试图做的是访问我动态添加的视图,并在某些触发器(如位置更新)上访问它

像下面这样的。。。我把代码简化了一点。但这应该会让你对我正在努力实现的目标有一个相当好的看法

  class MapViewController: UIViewController, UIScrollViewDelegate, UITextFieldDelegate, CLLocationManagerDelegate {
     //my custom views
     var mapPins = Set<UserPin>()
     override func viewDidLoad() {
         createScreen()
         updateScreen() //this happens on location update
         super.viewDidLoad()
    }

    func createScreen() {

        let userPinA = UserPin(userID: "ab12319ssdf91dfa2js92kajsdf23kd93", name: "J smith", posX: Int(1500), posY: Int(1800), pictureURL: "sad_face.jpg", direction: 1, colour: "Red", currentTrack: 33, lastTimeConnected: Date())
        mapPins.insert(userPinB)

        //this is actually done dynamically but good enough for the example
        mapPins.insert(userPinB)
        mapPins.insert(userPinC)
        mapPins.insert(userPinD)

        containerView = UIView()

        // adding is not a problem. So far so good!
        for userpin in mapPins
        {
            imageView.addSubview(userpin)
        }

        containerView.addSubview(imageView)
        scrollView.addSubview(containerView)
        view.addSubview(scrollView)
    }

    //triggered on location update, just believe it fires for the purpose of this
    func updateScreen()
    {
        //Here is the issue!
        //I want to get to the right view by userID, the same that is updated on my mapPins set, and update the posX and posY (animation is the intention). How do I do that?
        //something like self.view.subviews.view.userID, obviously that does not work. I just don't think I understand enough how custom views work to access the properties of them.

    }
您可以在视图控制器中创建一个字典,其中键是userid,对象是customview


如果有许多自定义视图,这会更有效,因为数组将需要大量迭代。

您有两个不错的选择:1将自定义视图数组作为VC的属性,2在子视图中实现KVO,我不建议这样做,因为在Swift中这会变得相当困难。好的,但这不是我的mapPins集吗?那么我如何访问它呢?我将通过@dylanthelion对答案添加两件事。1考虑在哪里可以使用标记属性/标识符,它可能是有用的。2我非常担心在viewDidLoad中执行任何名为updateScreen的操作。至少对我来说,这听起来像是你期望执行的频率比预期的要高。ViewWillDisplay、ViewDidDisplay和ViewWillLayoutSubView可能更合适。@user2091936:带有for…in循环。如果需要按特定顺序访问它们,请使用数组。如果您需要像这样过滤它们,请获取具有最高posX值的pin,使用filter:或reduce:。好的,我认为最简单的方法是在userid和标记之间创建映射。循环等对我来说并不难。
 import UIKit

    class UserPin: UIView {


        var userID : String?
        var name : String?
        var posX : Int?
        var posY : Int?
        var pictureURL : String?
        var direction : Int?
        var colour : String?
        var currentTrack : Int?
        var lastTimeConnected : Date?

        init(userID: String, name: String, posX: Int, posY: Int, pictureURL: String, direction: Int, colour: String, currentTrack : Int, lastTimeConnected : Date)
        {
            self.userID = userID
            self.name = name
            self.posX = posX
            self.posY = posY
            self.pictureURL = pictureURL
            self.direction = direction
            self.colour = colour
            self.currentTrack = currentTrack
            self.lastTimeConnected = lastTimeConnected

            super.init(frame: CGRect(x: posX, y: posY, width: 100, height: 100))
            self.addControls(userID, name: name, posX: posX, posY: posY,  pictureURL: pictureURL, direction: direction,  colour: colour,  currentTrack: currentTrack, lastTimeConnected: lastTimeConnected)
        }
     func addControls(_ userID: String, name: String, posX: Int, posY: Int, pictureURL: String, direction: Int, colour: String, currentTrack : Int, lastTimeConnected : Date) {
    //does stuff on the view with labels and shapes and stuff
    }