ラベル Swift の投稿を表示しています。 すべての投稿を表示
ラベル Swift の投稿を表示しています。 すべての投稿を表示

2014年12月12日金曜日

SwiftでOSXアプリ開発・ImageKit を使ってみる

写真のサムネイルを並べるために、NSCollectionView を使ってみたが
Xcode の ObjectLibrary (右下のやつ)を眺めてたら
ImageKit Image Browser View というのものがあった。
ちょっとぐぐってみると、スライダーでサムネイル画像の調節までできるとか、できないとか。

使ってみようとしたが

cannot decode object of class (IKImageBrowserView)
user of undeclared type 'IKImageBrowserView'

こんなエラー出る。

どうやら Quartz.framwork を Build Phases の Link Binary With Libraries でインポートしなければならないらしい。
更にoutlet とかしたい場合は、そのクラスの先頭で

import Quartz

を書けばOKだった。

環境
OSX Yosemite
Xcode6.1.1

2014年12月9日火曜日

SwiftでOSXアプリ開発・keyDownメソッドが呼ばれなくてハマったけど解決

NSView で KeyDown メソッドを定義したが
呼ばれない。

acceptsFirstResponder を override して
 true を返す必要があるらしいが
( Swiftのソースでは ) acceptsFirstResponder は入力補完で出てこないし
override するとエラーになるので
困っていたが
結局、下記のように記述すればOKだった。

入力補完されなかいからといって、そのメソッドが存在しないというわけではないらしい。



class View: NSView {

    func acceptsFirstResponder() -> Bool {
       
 return true
    }               

    override func keyDown(theEvent: NSEvent) {      
    }


環境
OSX Yosemite
XCode6.1.1



2014年12月8日月曜日

SwfitでOSXアプリ開発・プログラムからウィンドウを開く


NSWindowControllerのサブクラスを定義。

class ChildWindowController: NSWindowController {

ストーリーボード上にNSWindowControllerを配置して
カスタムクラスに、さっき定義した ChildWindowControllerを設定


開く処理

        var childWindowController:ChildWindowController = self.storyboard?.instantiateControllerWithIdentifier("ChildWindowController") as ChildWindowController

        self.view.window?.addChildWindow( childWindowController.window! , ordered: NSWindowOrderingMode.Above)

環境
OSX Yosemite
Xcode 6.1.1

2014年11月28日金曜日

SwiftでOSXアプリ開発・アプリのウィンドウをフルスクリーンにする

だいぶハマったので備忘録

まず、ストーリーボード上で、メニュー項目に、フルスクリーンを追加


そして、同じくストーリーボード上で
Window を選択して
Attributes inspecter

Full Screen
という項目があるので
Primary Window
を選択


無事、開発中のアプリでフルスクリーンできた。

参考サイト
http://ameblo.jp/firststepforward/entry-11508622193.html

環境
Xcode6.1
OSX Yosemite

2014年8月8日金曜日

Xcode6 ステータスバーの文字色を白くする

背景色の都合で、ステータスバー(キャリア名、アンテナ、時刻、バッテリーアイコン)の文字色を白くしたい

    override func preferredStatusBarStyle() -> UIStatusBarStyle{
        return UIStatusBarStyle.LightContent

    }

これを、ViewController.swift に追加しただけで白くなった。
備忘録。

環境
Xcode6 beta5

2014年8月7日木曜日

Xcode6 beta5 ではプロジェクトを作りなおしたほうがいいみたい

2014年8月6日にXcode6 beta5 が公開されていたようなので
インストールした。

Xcode6 beta4 で開発していたプロジェクトをそのまま
beta5 で開いたのだが...

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name:UIKeyboardWillHideNotification, object:1)

キーボードの通知でどうしてもクラッシュする

dyld: Symbol not found

beta5 で、新たにプロジェクトを作成して
処理などを移行したら、問題なく動いた。

何かXcodeバージョン間の移行手順などがあるのだろうか...



Swift エラー Type 'ViewController' does not conform to protocol 'UITableViewDataSource'


ViewController に、UITableView を追加した場合
下記のように、スーパークラスを定義する


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

ここで
Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

こんなエラーが出て少し嵌ったのでメモ

下記のような必要なメソッドを定義するとエラーは解消される

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->     func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

これ、つい忘れてしまう

環境
Xcode6 beta5

2014年7月11日金曜日

SwiftコードだけでUITableViewを配置

Storyboardを使わずに、Swiftコードだけで
UIViewController に UITableView を配置する例

( UITableView を配置する母体となる UIViewController は
Storyboard上に存在している前提です )


import UIKit

class HogeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableView : UITableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        tableView.frame = CGRectMake(0, 45, self.view.bounds.size.width, 1000)
        self.view.addSubview(tableView)
        
        tableView.delegate = self
        tableView.dataSource = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // セルの数
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int  {
        return 1
    }
    
    // セルの設定
    func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
        cell.textLabel.text = "セル \(indexPath.row)番目"
        cell.detailTextLabel.text = "詳細"
        return cell;
    }
    
    // セルが選択されたときに呼ばれる
    func tableView(tableView: UITableView?, didSelectRowAtIndexPath indexPath:NSIndexPath!) {
        println("選択された \(indexPath.row)番目")
    }

}

動作確認環境
Xcode6beta3

2014年7月10日木曜日

Swfitでアプリ内共通変数 ( グローバル変数? )



AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
                            
    var window: UIWindow?
        
    var userId:Int = 0     //  <==== この変数を他のクラスからアクセスする

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
        // Override point for customization after application launch.



ViewController.swift (他のクラス)

class ViewController: UIViewController {
                            
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // AppDelegateクラスのメンバー変数を参照する
        var app:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
        println(app.userId)

    }


動作環境
Xcode6beta3