小编Ami*_*mit的帖子

从Stackview获取View的索引

我添加了2个stackview具有不同大小的视图。现在,当我单击任何视图的加号按钮时,我只想在该视图的正下方添加一个视图。

我正在通过以下方式查看:

let index = superView.subviews.index(of: view)
Run Code Online (Sandbox Code Playgroud)

然后我在做:

superView.insertArrangedSubview(newView, index + 1)
Run Code Online (Sandbox Code Playgroud)

但实际上它是在不同位置添加新视图。

因此,如果我尝试获取索引,.subview.index那么它将始终返回最后一个索引

ios swift uistackview

3
推荐指数
1
解决办法
2466
查看次数

Swift 5 - Facebook 登录:安装 Facebook 应用程序时不会调用 LoginManager() 回调

我已经使用 cocoapods 将 Facebook SDK 更新到最新版本:

  • 使用FBSDKCoreKit (5.8.0)
  • 使用FBSDKLoginKit (5.8.0)
  • 使用 FacebookCore (0.9.0)
  • 使用 Facebook 登录 (0.9.0)

当安装 Facebook 应用程序时,LoginManager()不会调用回调。当未安装应用程序并使用浏览器时,它可以工作。

let loginManager = LoginManager()
loginManager.logIn(permissions: [.publicProfile, .email], viewController: self, completion: { loginResult in
    switch loginResult {
    case .failed(let error):
        print("\(error)")
    case .cancelled:
        print("cancelled")
    case .success(let grantedPermissions, let declinedPermissions, let accessToken):
        print("\(grantedPermissions) \(declinedPermissions)")
    }
})
Run Code Online (Sandbox Code Playgroud)

任何想法?

facebook facebook-login facebook-ios-sdk swift

3
推荐指数
1
解决办法
4345
查看次数

使用字典数据迭代字典并将其添加到swift中的数组中

我有一个包含多个字典数据的字典:

{
    1455201094707 =     {
    };
    1455201116404 =     {
    }: 
    1455201287530 =     {
    };
}
Run Code Online (Sandbox Code Playgroud)

我必须将所有这些字典添加到swift中的数组中.如何将字典迭代为:

for let tempDict in dataDictionary
{
     self.tempArray.addObject(tempDict)
}
Run Code Online (Sandbox Code Playgroud)

错误"让模式不能嵌套在已经不可变的上下文中"

for tempDict in dataDictionary as! NSMutableDictionary
{
     self.tempArray.addObject(tempDict)
}
Run Code Online (Sandbox Code Playgroud)

错误:参数类型元素(aka(键:AnyObject,值:AnyObject))参数类型不符合预期类型anyobject

for tempDict in dataDictionary as! NSMutableDictionary
{
     self.tempArray.addObject(tempDict as! AnyObject)
}
Run Code Online (Sandbox Code Playgroud)

错误:无法将类型'(Swift.AnyObject,Swift.AnyObject)'(0x1209dee18)的值转换为'Swift.AnyObject'(0x11d57d018).

for tempDict in dataDictionary
{
     self.tempArray.addObject(tempDict)
}
Run Code Online (Sandbox Code Playgroud)

错误:类型AnyObject的值没有成员生成器

编辑

我希望最终的数组为:

(
  {
    1455201094707 =     {
    };
  }
  {
     1455201116404 =     {
     }:
  } 
)   
Run Code Online (Sandbox Code Playgroud)

实现这个的正确方法是什么?

任何帮助将不胜感激.....

我用过代码:

var tempArray:[NSDictionary] = []

    for (key, …
Run Code Online (Sandbox Code Playgroud)

arrays iphone dictionary ios swift

2
推荐指数
1
解决办法
1842
查看次数

popToViewController 不工作,但 popViewController 工作

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'
Run Code Online (Sandbox Code Playgroud)

使用此代码时

self?.navigationController?.popToViewController(vc2, animated: true)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试弹出它的工作原理时

self?.navigationController?.popViewController(animated: true)
Run Code Online (Sandbox Code Playgroud)

我正在推动这个 viewController 就像

navigationController?.pushViewController(vc2, animated: true)
Run Code Online (Sandbox Code Playgroud)

我不确定,推动观点意味着当我尝试做时popToViewController。它上面有一个视图。请帮忙

uiviewcontroller uinavigationcontroller ios swift

2
推荐指数
1
解决办法
1840
查看次数

呈现视图控制器时设置后退按钮

单击按钮后,我将使用以下代码显示视图控制器:

let navController = UINavigationController(rootViewController: locationVC)
navController.navigationBar.barTintColor = StyleHelper.navBarColor()
navController.navigationBar.tintColor = UIColor.whiteColor()               
navController.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "back_arrow"), style: .Plain, target: locationVC, action: nil)
self.presentViewController(navController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

但是后退按钮没有出现在locationVC上

我究竟做错了什么?请帮忙

uinavigationbar ios swift

2
推荐指数
2
解决办法
5318
查看次数

UISlider无法正确处理.touchUpInside事件

在过去的一天中,我遇到了一个困扰我的问题,而我只是想不出解决方案...

我有一个UISlider,范围从值1到3(这就是我们需要的方式)。滑块是连续的,因为它实际上根据其当前值更新一些属性。现在,我想实现一个功能,其中滑块根据用户开始触摸或结束触摸来触发动作。

我尝试使用该.touchDown方法来检测触摸的开始,效果很好。但是,当我尝试使用.touchUpInside.touchUpOutside检测用户何时松开手指时,该事件在适当的时间不会触发。

当我在滑杆上拖动手指(距离其起点一定距离)时,事件触发。但是当我短暂地触摸它或移动它很少时,触摸事件根本不会触发...

有人可以帮我吗?这是我用来设置滑块的代码:

let slider: UISlider = {
    let slider = UISlider()
    slider.isContinuous = true
    slider.minimumValue = 1
    slider.maximumValue = 3
    slider.setMinimumTrackImage(#imageLiteral(resourceName: "minimumSliderTrackImage").resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)), for: .normal)
    slider.setMaximumTrackImage(#imageLiteral(resourceName: "maximumSliderTrackImage").resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)), for: .normal)

    let sliderThumb = #imageLiteral(resourceName: "sliderThumbImage")
    slider.setThumbImage(sliderThumb, for: .normal)

    slider.addTarget(self, action: #selector(adjustPriceLabel), for: .valueChanged)
    slider.addTarget(self, action: #selector(handleTouchDown), for: .touchDown)
    slider.addTarget(self, action: #selector(handleTouchUp), for: [.touchUpInside, .touchUpOutside])

    slider.setValue(2.0, …
Run Code Online (Sandbox Code Playgroud)

event-handling uislider ios swift swift4

2
推荐指数
1
解决办法
791
查看次数

如何使用 Dispatch Groups 等待调用依赖于不同数据的多个函数?

我有三个变量abc。我有三个带有完成块的异步函数来更新这些变量,另外三个函数只处理一些数据。

我确保工作函数等到所有数据都更新为DispatchGroup.

// The Data
var a: String?
var b: String?
var c: String?

// The Update
let group = DispatchGroup()

group.enter()
updateA() {
    group.leave()
}

group.enter()
updateB() {
    group.leave()
}

group.enter()
updateC() {
    group.leave()
}

group.wait()

// The work
doSomthingWith(a: a, b: b)
doSomethingElseWith(b: b, c: c)
doAnotherThingWith(a: a, c: c)
Run Code Online (Sandbox Code Playgroud)

我希望能够做的是在参数更新后调用每个工作函数,而不是等待一切。这只是一个(显然)简化版本。可能有更多的变量和函数。

我正在使用斯威夫特。提前谢谢了。

grand-central-dispatch swift dispatchgroup

2
推荐指数
1
解决办法
1869
查看次数

UITableview reloadRows 在 Swift 3 中无法正常工作

我创建了一个UITableView包含两个具有动态单元高度的单元的多个部分。在第二个单元格上添加了一个按钮。单击按钮后,通过更改约束高度来增加第二个单元高度。连接到按钮的操作方法是:

func increaseHeightOnClickOfButton(sender: UIButton) {
    let indexPath = IndexPath(item: 1, section: sender.tag)

    let cell = myTableView.cellForRow(at: indexPath) as! customCell
    cell.viewHeightConstraint.constant = 100

    self.myTableView.reloadRows(at: [indexPath], with: .automatic)
}
Run Code Online (Sandbox Code Playgroud)

问题:单击按钮时,第二部分的单元格高度会增加,而不是第一个部分的高度。

我也尝试过给出这样的部分的固定值:

let indexPath = IndexPath(item: 1, section: 0)
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

仅当我尝试此代码时它才有效:

func increaseHeightOnClickOfButton(sender: UIButton) {
    let indexPath = IndexPath(item: 1, section: sender.tag)

    let cell = myTableView.cellForRow(at: indexPath) as! customCell
    cell.viewHeightConstraint.constant = 100

    self.myTableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)

但我不想重新加载整个表视图。任何人都可以帮助我做错了什么或者有其他方法可以实现它吗?

编辑

我又做了一项更改,删除了:

self.myTableView.reloadRows(at: [indexPath], with: .automatic)
Run Code Online (Sandbox Code Playgroud)

当我开始滚动表格视图时单击按钮后,返回到同一单元格会增加其高度。所以我猜想有一些问题reloadRows

uitableview ios swift

1
推荐指数
1
解决办法
3980
查看次数

自定义集合视图单元无法正常工作

尝试创建自定义单元格,但是当我在单元格collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath)中添加标签时不起作用。

没有标签单元格 UI:

在此处输入图片说明

iPad 模拟器 UI 是:

在此处输入图片说明

但是当我在单元格中添加标签时:

在此处输入图片说明

iPad 模拟器 UI 变为:

在此处输入图片说明

UICollectionViewDelegateFlowLayout方法中:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let emptySpace = 87*2 + 10
    let remainingWidth = Int(self.view.frame.width) - emptySpace
    return CGSize(width: remainingWidth/2, height: remainingWidth/2)
}
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell swift

1
推荐指数
1
解决办法
940
查看次数

了解SpriteKit CollisionBitMask

我正在学习使用SpriteKit,我正在学习colllisions教程.我正在努力理解以下代码:

struct PhysicsCategory {
  static let None      : UInt32 = 0
  static let All       : UInt32 = UInt32.max
  static let Monster   : UInt32 = 0b1       // 1
  static let Projectile: UInt32 = 0b10      // 2
}
Run Code Online (Sandbox Code Playgroud)

为什么我们要分配这些被调用的东西bitMaps以及它们如何在下面的代码中工作?:

func didBegin(_ contact: SKPhysicsContact) {

    // 1
    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    // 2
    if ((firstBody.categoryBitMask & …
Run Code Online (Sandbox Code Playgroud)

xcode sprite-kit swift

0
推荐指数
1
解决办法
632
查看次数

添加一个 BlockOperation 对另一个 BlockOperation 的依赖在 swift 中无法正常工作

我在控制器中有多个 api,成功响应后我必须重新加载UITableView.

现在我从两个 api 开始,第二个 api 依赖于第一个 usingBlockOperationDispatchGroupin it。

首先在viewDidLoad

getDataFromAllApis {
    self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)

然后我添加了方法:

func getDataFromAllApis(completion: @escaping (() -> Void)) {

    let queue = OperationQueue()

    let getFirstDataOperation = BlockOperation {
        let group = DispatchGroup()
        group.enter()
        self.getFirstDataFromApi {
            group.leave()
        }
        group.wait()
    }
    queue.addOperation(getFirstDataOperation)

    let getSecondDataOperation = BlockOperation {
        let group = DispatchGroup()
        group.enter()
        self.getSecondDataFromApi {
            group.leave()
        }

        group.notify(queue: .main) {
            completion()
        }
    }
    queue.addOperation(getSecondDataOperation)

    getSecondDataOperation.addDependency(getFirstDataOperation)
}
Run Code Online (Sandbox Code Playgroud)

我在这里面临的问题是 getSecondDataOperation 首先执行并返回到 tableview 重新加载部分。

我在这里遗漏了什么还是可以有不同的方法?任何帮助将不胜感激。 …

ios swift blockoperation dispatchgroup

0
推荐指数
1
解决办法
369
查看次数