ios – 为数组中的每个对象添加按钮

ios – 为数组中的每个对象添加按钮,第1张

概述我有一个对象数组,我想为数组中的每个对象提供一个按钮.我做得对吗?按钮没有显示在视图上,我不认为正在调用for循环.我希望按钮处于随机位置,每个按钮都不同.我究竟做错了什么?我使用解析来检索距离点一定距离的对象,这是有效的. var locationarray = [AnyObject]()var userbutton = [UIButton]() override func viewWi 我有一个对象数组,我想为数组中的每个对象提供一个按钮.我做得对吗?按钮没有显示在视图上,我不认为正在调用for循环.我希望按钮处于随机位置,每个按钮都不同.我究竟做错了什么?我使用解析来检索距离点一定距离的对象,这是有效的.

var locationarray = [AnyObject]()var userbutton = [UIbutton]()  overrIDe func vIEwWillAppear(animated: Bool) {    let userlocation = PFUser.query()    PFGeoPoint.geoPointForCurrentLocationInBackground(){        (point:PFGeoPoint?,error:NSError?) -> VoID in        NSLog("Test log 1") // Never printed        if point != nil {            // Succeeding in getting current location            NSLog("Got current location successfully") // never printed            PFUser.currentUser()!.setValue(point,forKey: "userlocation")            PFUser.currentUser()!.saveInBackground()        } else {            // Failed to get location            NSLog("Failed to get current location") // never printed        }        var query = PFUser.query()        query?.whereKey("userlocation",nearGeoPoint: point!,withinMiles: 2.0)        query?.findobjectsInBackgrounDWithBlock({ (objects: [AnyObject]?,error: NSError?) -> VoID in            if(error == nil){                for object in objects! {                  self.locationarray.append(object)                  print(self.locationarray)                  }               }else{                print(error)            }        })    }    for users in locationarray{        let button = UIbutton()        button.addTarget(self,action: "buttonAction:",forControlEvents: .touchUpInsIDe)        button.frame = CGRectMake(100,100,50)        let buttonWIDth = button.frame.wIDth        let buttonHeight = button.frame.height        // Find the wIDth and height of the enclosing vIEw        let vIEwWIDth = button.supervIEw!.bounds.wIDth        let vIEwHeight = button.supervIEw!.bounds.height        let xwIDth = vIEwWIDth - buttonWIDth        let yheight = vIEwHeight - buttonHeight        // Generate a random x and y offset        let xoffset = CGfloat(arc4random_uniform(UInt32(xwIDth)))        let yoffset = CGfloat(arc4random_uniform(UInt32(yheight)))        // Offset the button's center by the random offsets.        button.center.x = xoffset + buttonWIDth / 2        button.center.y = yoffset + buttonHeight / 2        self.userbutton.append(button)        print("called")        print(self.userbutton)        self.vIEw.addSubvIEw(button)    }}
解决方法 我在你的代码中发现了问题 – findobjectsInBackgrounDWithBlock是异步函数,你的位置数组循环在findobjectsInBackgrounDWithBlock完成之前被调用.并且由于此时locationArray为空 – 不调用for循环.

试试这个,使用这种方法,只有当从服务器接收到所有对象时才调用locationsSet.

overrIDe func vIEwWillAppear(animated: Bool) {  let userlocation = PFUser.query()  PFGeoPoint.geoPointForCurrentLocationInBackground(){    (point:PFGeoPoint?,error:NSError?) -> VoID in    NSLog("Test log 1") // Never printed    if point != nil {      // Succeeding in getting current location      NSLog("Got current location successfully") // never printed      PFUser.currentUser()!.setValue(point,forKey: "userlocation")      PFUser.currentUser()!.saveInBackground()    }else{      // Failed to get location      NSLog("Failed to get current location") // never printed    }    var query = PFUser.query()    query?.whereKey("userlocation",withinMiles: 2.0)    query?.findobjectsInBackgrounDWithBlock({ (objects: [AnyObject]?,error: NSError?) -> VoID in      if(error == nil){        for object in objects! {          self.locationarray.append(object)          print(self.locationarray)        }        self.locationsSet()       }else{         print(error)       }    })  }}func locationsSet(){  for users in locationarray{    let button = UIbutton()    button.addTarget(self,forControlEvents: .touchUpInsIDe)    button.frame = CGRectMake(100,50)    let buttonWIDth = button.frame.wIDth    let buttonHeight = button.frame.height    // Find the wIDth and height of the enclosing vIEw    let vIEwWIDth = self.vIEw.bounds.wIDth    let vIEwHeight = self.vIEw.bounds.height    let xwIDth = vIEwWIDth - buttonWIDth    let yheight = vIEwHeight - buttonHeight    // Generate a random x and y offset    let xoffset = CGfloat(arc4random_uniform(UInt32(xwIDth)))    let yoffset = CGfloat(arc4random_uniform(UInt32(yheight)))    // Offset the button's center by the random offsets.    button.center.x = xoffset + buttonWIDth / 2    button.center.y = yoffset + buttonHeight / 2    self.userbutton.append(button)    print("called")    print(self.userbutton)    self.vIEw.addSubvIEw(button)   }}
总结

以上是内存溢出为你收集整理的ios – 为数组中的每个对象添加按钮全部内容,希望文章能够帮你解决ios – 为数组中的每个对象添加按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1028536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存