标签: swift2

将 xib 文件与 UIViewController 和自定义类链接

为了创建 xib 文件,我添加了一个新的 UIViewController 的可可触摸文件子类,并选中了“也创建 XIB 文件”。

我的 xib 文件中只有一个标签。

在我的视图控制器中:

import UIKit

class TestControllerViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    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.
    }
Run Code Online (Sandbox Code Playgroud)

除了我的 IBoutlet 之外别无他物(我至少检查了连接检查器 10 次)。

和我的自定义类:

import Foundation
import UIKit


class Card: UIView {
    internal var number:Int?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) …
Run Code Online (Sandbox Code Playgroud)

xib uiview swift swift2

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

传递#selector Swift 2.2中的Argument

我有一个方法:

func followUnfollow(followIcon: UIImageView, channelId: String) {
    let followUnfollow = followIcon
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.followIconTapped(_:)))
    followUnfollow.userInteractionEnabled = true
    followUnfollow.addGestureRecognizer(tapGestureRecognizer)
}
Run Code Online (Sandbox Code Playgroud)

而且我有一个方法:

func followIconTapped(sender: UITapGestureRecognizer) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

它的工作正常.但我需要传递channelIdfollowIconTapped()方法.

我试试这个:

func followUnfollow(followIcon: UIImageView, channelId: String) {
    ...
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.followIconTapped(_:channelId)))
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试抓住它:

func followIconTapped(sender: UITapGestureRecognizer, channelId: String) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

xCode说channelId永远不会使用.为什么?当我构建项目时,我没有任何问题.但是,如果我点击followIcon, app就会崩溃.

拜托,你可以给我建议如何在传递channelIdfollowIconTapped()

ios swift2

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

如何使用SwiftyJson解析JSON

这是数据

[{
  "mineral": "Phosphorus",   
  "data": [ 7.65, 19.74, 15.48 ]
},

{
  "mineral": "Calcium",   
  "data": [ 1.65, 1.32, 1.78 ]
}]
Run Code Online (Sandbox Code Playgroud)

这是我用来解析它的代码.

if let dataFromString = self.response.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {

        let json = JSON(data: dataFromString)

        var valuesB = [Double]()

        for i in 0..<json.count {
            self.nutrient.append(json[i]["mineral"].stringValue)
            print("Nutrient: \(json[i]["mineral"].stringValue)\n")

            for j in 0..<json[i]["data"].count {
                valuesB.append(json[i]["data"][j].doubleValue)
                print("Values: \(json[i]["data"][j].doubleValue)")
            }

            values.append(valuesB)
        }
   }
Run Code Online (Sandbox Code Playgroud)

但我得到的只是

Nutrient: Phosphorus

Values: 0.0
Values: 0.0
Values: 0.0

Nutrient: Calcium

Values: 0.0
Values: 0.0
Values: 0.0
Run Code Online (Sandbox Code Playgroud)

我正在使用SwiftyJson将Xcode 7与Swift一起使用,还有另外一种方法可以使用SwiftyJson吗?

xcode json swift swifty-json swift2

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

无法解释'|' 字符

我需要你的帮助,因为我不明白我的自动约束是怎么回事.我的开关的限制使应用程序崩溃.当我删除它时,它工作得很好.这是我得到的错误消息:无法解释'|' 字符,因为相关视图没有超视图H:| -100- [v0(35)] |

谢谢你的帮助

这是我的代码:

class selectionCustomCell: UITableViewCell{
    var label: UILabel = {
        let attribution = UILabel()
        attribution.text = "Nom du label"
        attribution.textColor = UIColor(r: 0, g: 185, b: 255)
        attribution.lineBreakMode = NSLineBreakMode.ByWordWrapping
        attribution.numberOfLines = 0
        attribution.translatesAutoresizingMaskIntoConstraints = false
        return attribution
    }()

  var switchElement: UISwitch{
        let sL = UISwitch()
        sL.setOn(true, animated: true)
        sL.onTintColor = UIColor(r: 0, g: 185, b: 255)
        sL.tintColor = UIColor(r: 0, g: 185, b: 255)
        sL.translatesAutoresizingMaskIntoConstraints = false
        return sL
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: …
Run Code Online (Sandbox Code Playgroud)

nsautolayout swift swift2

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

如何为第二个ViewController添加代码文件

请原谅我的问题。除了第一个默认ViewController及其伴随文件之外ViewController.swift,我还创建了第二个viewcontroller并知道如何移动到它。如何SecondViewController.swift为其添加同伴?

谢谢

xcode7 swift2

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

Firebase控制台中未显示事件日志参数

我为我的iOS应用实施了Firebase Analytics,并在GoogleServicec-info.plist中设置了IS_ANALYTICS_ENABLED,

我尝试记录3个不同的事件来测试Firebase分析

FIRAnalytics.logEventWithName("tap_cart", parameters: ["userId":userId,"productId":productId] )
FIRAnalytics.logEventWithName("tapCart", parameters: [kFIRParameterItemID:productId,kFIRParameterContentType: username, "userId":userId, kFIRParameterValue: productName])
FIRAnalytics.logEventWithName("tap_buy", parameters: [kFIRParameterContentType: username,kFIRParameterItemID: productId])
Run Code Online (Sandbox Code Playgroud)

在等待几个小时(可能3-4个小时)之后,我的Firebase控制台会显示这些事件(tap_cart,tapCart和tap_buy),但当我查看详细信息时,这些都没有显示参数.

我的预期结果就像这张图片

我希望看到基于我/她点击购买的用户和产品的图表,我在这里缺少什么?

ios swift2 firebase-analytics

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

Facebook登录不首次工作 - ios

我将Facebook SDK集成到我的ios应用程序中.这是我使用的代码:

let loginManager = FBSDKLoginManager()
loginManager.loginBehavior = FBSDKLoginBehavior.Browser
loginManager.logInWithReadPermissions(["public_profile", "email"], fromViewController: context, handler: {(result, error) -> Void in
    //My handling code
})
Run Code Online (Sandbox Code Playgroud)

当我第一次尝试登录Facebook时,安装应用程序后,它通过我的代码就好了,但没有任何反应 - 浏览器没有出现,处理程序没有触发,没有错误,日志中没有任何内容.之后,即使我强行退出应用程序,每次尝试登录时都会有效.

我不太确定,如果这个问题出现是因为我做了什么,或者是从第一天起问题.

facebook ios swift swift2 fbsdk

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

在Swift中更改UIProgressView(Bar样式)的高度

我在Swift 2.2中创建了一个UIProgressView(条形样式),现在想改变它的高度.建议使用其他一些帖子CGAffineTransformScale来改变身高.但是,出于某种原因,这种方法似乎不起作用,因为它对UIProgressView的高度没有任何影响.

您将在下面找到我的代码:

let progressBar = UIProgressView(progressViewStyle: .Bar)
progressBar.progress = 0.5
progressBar.translatesAutoresizingMaskIntoConstraints = false
progressBar.transform = CGAffineTransformScale(progressBar.transform, 1, 20)
self.navigationItem.titleView = progressBar
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,非常感谢!

height uiprogressview ios swift swift2

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

如何使用Swift在Appdelegate中获取当前位置

嗨,我正在使用Swift开发应用程序,我想在应用程序启动时获取用户的当前位置,因此我在应用程序委托中编写了代码,其中包括所有功能和方法。添加和导入框架的核心位置,也更新了plist,但我无法获取当前位置

我的应用程序委托中的代码:

import UIKit

 import CoreLocation


import MapKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate,CLLocationManagerDelegate {
var locationManager:CLLocationManager!
var window: UIWindow?
  var centerContainer: MMDrawerController?


  private var currentCoordinate: CLLocationCoordinate2D?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
     IQKeyboardManager.sharedManager().enable = true
    self.locationManager = CLLocationManager()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    determineMyCurrentLocation()

    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    if (configureError != nil){
        print("We have an error:\(configureError)")
    }
    GIDSignIn.sharedInstance().clientID = "331294109111-o54tgj4kf824pbb1q6f4tvfq215is0lt.apps.googleusercontent.com"

    GIDSignIn.sharedInstance().delegate = self
    return true …
Run Code Online (Sandbox Code Playgroud)

mkmapview ios currentlocation swift swift2

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

UICollectionView cellForItem调用不正确

将Swift 2.2迁移到Swift 3之后的Xcode 8.2.1

UICollectionView的cellForItem未正确调用但在迁移之前工作正常

我有CollectionView与水平UICollectionViewFlowLayout并设置分页设置为true

UIViewController的视图框架与UICollectionView的框架相同,也是像网页应用程序一样的单元框架(全尺寸)

cellForItem当我向右滚动UICollectionView时,我在(UICollectionViewDataSource func)中打印indexPath.row

indexPath.row打印如下

0, 3, 4, 5, 6, 7, 8, 9, 10

然后在最后打印时滚动到相反的方向 10

下一个indexPath 7但它应该是9因为最后调用的索引是10

并滚动直到第一个单元格显示

6, 5, 4, 3, 2, 1, 0 印刷的

为什么会出现这种情况?

在迁移之前的Swift 2.2中,工作正常

在Swift 3或iOS 10之后是否有更改为UICollectionView或UICollectionViewFlowLayout?

ios uicollectionview swift2 swift3

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