自分用の覚え書きです。
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)
}