3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift覚え書き

Last updated at Posted at 2016-03-31

自分用の覚え書きです。
Swift1.x混ざってるかも。

AppDelegateをインスタンス化

private let myApp:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

Segueを使わず、コードで遷移

let userRegistViewController:UserRegistViewController = self.storyboard!.instantiateViewControllerWithIdentifier("UserRegistView") as! UserRegistViewController
self.presentViewController( userRegistViewController, animated: false, completion: nil)
Segueで遷移時に値の受け渡し方

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "SegueIdx") {
        let controller:SecondViewController = segue.destinationViewController as! SecondViewController
            // controller.idx = 1
        }
    }

StoryBoad上でカスタムUIの反映

IBDesignablでカスタムクラスを作ってStoryBoadに適用する

例えば角丸ボタンができる

import UIKit

@IBDesignable

class RadiusUIButton: UIButton {

    @IBInspectable var textColor: UIColor?

    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var borderColor: UIColor = UIColor.clearColor() {
        didSet {
            layer.borderColor = borderColor.CGColor
        }
    }

}

Bridge-Header Sample

#ifndef catalogApp_Bridging_Header_h
#define catalogApp_Bridging_Header_h

//#import "AFNetworking.h"
//#import "AFNetworkActivityLogger.h"

#import "MWFeedParser.h"
#import "SVProgressHUD.h"
#import "sqlite3.h"

#import <GoogleMobileAds/GoogleMobileAds.h>
//#import "GADBannerView.h"
//#import "GADInterstitial.h"

//#import <GoogleAnalytics-iOS-SDK/GAI.h>
//#import <GoogleAnalytics-iOS-SDK/GAIFields.h>
//#import <GoogleAnalytics-iOS-SDK/GAILogger.h>
//#import <GoogleAnalytics-iOS-SDK/GAIDictionaryBuilder.h>
#endif
  • BuildSettings > Objective-C Bridge Header
  • appName/Bridge-Header.h

ステータスバーの色

info.plistに以下2つのkeyとvalueを追加する

View controller-based status bar appearance
  • NO : アプリ全体で設定
  • YES : ViewControllerごとに設定
Status bar style
  • Gray style (default) : 文字色が黒になる
  • Transparent black style (alpha of 0.5) : 文字色が白になる(iOS7以降では非対応)
  • Opaque black style : 文字色が白になる(iOS7以降では非対応)
  • UIStatusBarStyleLightContent : 文字色が白くなる(iOS7以降)

汎用アラート

func showAlert( title:String = "title", text:String = "text") {

    let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction( UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
        (alert: UIAlertAction!) in

    })
    )

    self.presentViewController(alert, animated: true, completion: nil)
}

GCD便利用

dispatch_async_global {

    // サブスレッド

    self.dispatch_async_main {

        // メインスレッド

    }
}

func dispatch_async_main(block: () -> ()) {
    dispatch_async(dispatch_get_main_queue(), block)
}

func dispatch_async_global(block: () -> ()) {
       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
}
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?