小编tkt*_*ota的帖子

在swift中覆盖UIView的init方法

class CustomView: UIView {

    var subViewColor:UIColor
    var subViewMessage:String

    override init(frame:CGRect) {
        super.init(frame:frame)
    }

    init(subViewColor:UIColor,subViewMessage:String){

        self.subViewColor = subViewColor
        self.subViewMessage = subViewMessage
        super.init()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
Run Code Online (Sandbox Code Playgroud)

我有一个类,我希望用户通过提供以下属性来初始化自定义视图:

let myView = CustomLoadingView(initialize properties here)
Run Code Online (Sandbox Code Playgroud)

如果用户不想初始化自己的属性,我想CustomLoadingView使用默认属性进行初始化...

let myView = CustomLoadingView() // this should initialize using default value
Run Code Online (Sandbox Code Playgroud)

但是,有了这个,我收到了这个错误:

必须调用超类UIView的指定初始化程序

uiview ios swift swift2

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

使用Swift 3解析JSON响应

我有这样的JSON:

{"posts":
    [
    {
    "id":"1","title":"title 1"
    },
    {
    "id":"2","title":"title 2"
    },
    {
    "id":"3","title":"title 3"
    },
    {
    "id":"4","title":"title 4"
    },
    {
    "id":"5","title":"title 5"
    }
    ],
    "text":"Some text",
    "result":1
}
Run Code Online (Sandbox Code Playgroud)

如何使用Swift 3解析JSON?

我有这个:

let url = URL(string: "http://domain.com/file.php")!
let request = URLRequest(url: url)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data else {
        print("request failed \(error)")
            return
    }

    do {
        if let json = try JSONSerialization.jsonObject(with: data) as? [String: String], let result = json["result"] { …
Run Code Online (Sandbox Code Playgroud)

json ios swift swift3

11
推荐指数
2
解决办法
4万
查看次数

Segue到另一个故事板Swift

我试图以编程方式转换到另一个故事板,但每次我尝试使用没有内容的黑屏时加载视图.

这是我一直在使用的代码:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let startingView = storyboard.instantiateViewControllerWithIdentifier("HomeScreenView")
self.showViewController(startingView, sender: self)
Run Code Online (Sandbox Code Playgroud)

我在Destination Storyboard的Origin的故事板上创建了引用,并且还尝试调用:

performSegueWithIdentifier("changeover", sender: self)
Run Code Online (Sandbox Code Playgroud)

任何想法和建议将不胜感激.

ios xcode-storyboard swift

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

如何让 iOS navigationItem.titleView 左对齐?

navigationItem.hidesBackButton  = true
navigationItem.leftBarButtonItem = nil

head = UIView()
head.frame = CGRectMake(0, 0, 200, 44)
head.frame.origin.x = CGFloat(0)
navigationItem.titleView = head
Run Code Online (Sandbox Code Playgroud)

我尝试将 titleView 与左侧对齐,但它仍然保持在中间。

ios swift

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

为什么我的 SpriteKit update() 函数会创建多个 NSTimer?

我正在创建一个 SpriteKit 游戏,它根据经过的时间进行更新。游戏使用NSTimer及其scheduledTimerWithTimeInterval方法生成敌人,每 2.0 秒调用该spawnEnemy函数。

5 秒过去后,应该有一个非常短暂的间歇,防止新敌人生成,以显示关卡变化动画。

当到达最初的 5 秒时,一切都运行良好,直到条件条件 where 为止self.nextLevelDelayTicker == 100。一旦满足此条件,该"YOLO"字符串仅在控制台中触发一次。但是,我假设NSTimer正在创建并存储多个实例,因为在调用创建新的预定计时器self.timer后会产生大量敌人。self.resumeGame()

即使我在条件中设置了仅调用该self.resumeGame()函数一次的标志,关于为什么会发生这种情况的任何想法吗?

func resumeGame() {
    // Start game timer
    // Need a way to access ib action of pause button
    self.timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "spawnEnemy", userInfo: nil, repeats: true)
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if gameTicker.isActive == true { …
Run Code Online (Sandbox Code Playgroud)

ios sprite-kit swift

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

如何在AppDelegate外注册推送通知?

我已经设置推送通知并在appdelegate中工作,但是一旦应用程序打开,我就不会让它们启动,我希望它们在用户注册应用程序后在单独的"允许推送通知"视图控制器上启动.我尝试appDelegate通过这样做来引用:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
Run Code Online (Sandbox Code Playgroud)

我有一个registerForPushNotifications功能appDelegate:

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}
Run Code Online (Sandbox Code Playgroud)

didFinishLaunchingWithOptions函数内部调用时有效,AppDelegate但是当我尝试在我的视图控制器上调用该函数时,我没有任何东西可以传递给函数的应用程序部分,所以我最终得到一个错误.

@IBAction func yesButtonPressed(sender: AnyObject) {
    appDelegate.registerForPushNotifications(application)
    performSegueWithIdentifier("pushToHomeSegue", sender: self)
}
Run Code Online (Sandbox Code Playgroud)

push-notification apple-push-notifications ios appdelegate swift

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

如何在UILabel中选择特定单词(Swift 2.0)?

我一直面临一个大问题,可能整天都有一个简单的解决方案.我有一个UILabel,它包含一个NSAttributedString.我想要一个特定的范围UILabel来进行点播.我不想要任何第三方功能.

这是我的示例代码:

let str = dict["itemName"] as! NSString
range = string.rangeOfString(dict["itemName"] as! String)!
index = string.startIndex.distanceTo(range.startIndex)
attributedString.addAttribute(NSForegroundColorAttributeName, value: color, range: NSRange(location: index, length: str.length))
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios swift

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