0
3

More than 3 years have passed since last update.

[Swift] コードで書くUIView、 ボタンを押すと画面遷移 備忘録

Last updated at Posted at 2020-03-05

override func viewDidLoad() {
  super.viewDidLoad()
  Button()
}

func Button() -> UIView?{
  let button = UIButton(frame: CGRect(x:self.view.frame.maxX - 50, y:0, width:50, height: 50))
  button.backgroundColor = UIColor.black
  button.setTitle("追加", for: .normal)
  //追加ボタンがタップされた際に画面遷移をする(buttonTappedはタップされた際に呼び出される関数)
  button.addTarget(self, action: #selector(ViewController.buttonTapped(sender:)), for: .touchUpInside)
  view.addSubview(button)
  return view
}

@objc func buttonTapped(sender:UIButton){
  let storyboard: UIStoryboard = self.storyboard!
  let second = storyboard.instantiateViewController(withIdentifier: "secondView")
  self.present(second, animated: true, completion: nil)
}

StoryboardIDを記入
スクリーンショット 2020-03-05 17.49.55.png

以上

追記
このやり方よりも、R.swiftは画面遷移にとても便利
R.Swift使い方 備忘録

0
3
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
0
3