Swiftでalloc相当のことをやってみる
Obj-cで言うところのalloc initはどうするのかやってみた
違和感のあったのは
グローバル宣言時でもalloc initできちゃう部分
self.を入れなくてはいけない部分と入れなくてもイイ部分
各種プロパティの設定時に!が必要な部分と不要な部分の違い
クラスに外部参照できるプロパティ相当のものを設定する方法は未だ不明。
Delegateを設定する方法も知りたいなぁ
import UIKit
class ViewController: UIViewController {
//NewVCは自作UIViewControllerクラス
var newc : NewVC? //= NewVC.init(nibName:"NewVC",bundle:nil)
var newv : NewVC? = NewVC(nibName:"NewVC",bundle:nil)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// newc = NewVC[[alloc]initWithNibName:"NewVC" bundle:nil];
newc = NewVC(nibName:"NewVC", bundle:nil)//initは.initと入れない
//[self.view addsubView:newc.view];
self.view.addSubview(newc!.view)//newcの後に!を入れないとエラー
newc!.view.frame = CGRectMake(5,5,300,300)//mewcの前にself.を入れなくてもOK
//.sizeと.positionを別々に入れたい場合はどうする??(サイズだけ変更して位置は動かしたくないとき等)
self.view.addSubview(newv!.view)
newv!.view.backgroundColor = UIColor.redColor()
newv!.view.frame = CGRectMake(12,310,200,160)//newvの前にself.を入れなくてもOK
UIView.animateWithDuration(4.0,animations:{
self.newv!.view.frame = CGRectMake(12,460,200,160)
self.newv!.view.alpha = 0//newvの前にself.を入れないとエラー
}, completion: {(finished:Bool) in
//self.newv!.view.hidden=true
self.newv!.view.alpha = 1//newvの前にself.を入れないとエラー
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}