안녕하세요 후르륵짭짭입니다.
이제 StoryBoard를 사용하지 않고 가능하면 Code를 사용해서 UIKit을 구성하려고 합니다.
그럼 바로 가시죠!!
** Main StoryBoard를 삭제했다면 **
만약에 StoryBoard를 삭제하고 나서 실행 버튼을 누르면
'Could not find a storyboard named 'Main' in bundle NSBundle'
이런 오류가 뜨는데 해결 방법은 아래와 같습니다.
이렇게 General 부분에 들어가서 Main Interface를 지우고
Info.plist에 들어가서 위와 같이 링크를 타고 난 다음에 Storyboard Name을 지워버립니다.
** SceneDelegate에 RootView 등록 해주기 **
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
// window?.rootViewController = UINavigationController(rootViewController: ViewController())
window?.rootViewController = MainNavigationController()
window?.makeKeyAndVisible()
}
여기서 일반적인 Navigation을 시작하려면
window?.rootViewController = UINavigationController(rootViewController: ViewController())
이렇게 해주면 되는데, 우리는 CutomNavigation을 만들거니깐
NavigationContoller 클래스를 생성해줍니다.
** Navigation Controller 생성 **
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
print("Navigation Contoller")
let vc = BlueViewController()
self.pushViewController(vc, animated: true)
}
}
이때, 보통 아래 처럼 해주면 안됩니다.
navigationController?.pushViewController(vc, animated: true)
클래스 자체가 Navigation이기 때문에 self를 넣어주면 됩니다.
self.pushViewController(vc, animated: true)
** Navigation에서 데이터 전송해서 보내기 **
class BlueViewController: UIViewController {
let button : UIButton = {
let button = UIButton()
button.setTitle("클릭 here", for: .normal)
button.setTitleColor(.yellow, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(pushNavi), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .blue
}
override func viewDidLayoutSubviews() {
setbutton()
}
private func setbutton(){
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.heightAnchor.constraint(equalToConstant: 100),
button.widthAnchor.constraint(equalToConstant: 100)
])
}
@objc private func pushNavi(){
let vc = ViewController()
vc.getData = "Hello World"
navigationController?.pushViewController(vc, animated: true)
}
}
이렇게 되어 있을 때, 간단하게 아래 처럼 객체를 생성한 다음에 pushViewController를 넣어주면 됩니다.
@objc private func pushNavi(){
let vc = ViewController()
vc.getData = "Hello World"
navigationController?.pushViewController(vc, animated: true)
}
참고 사이트 :
StoryBoard 없이 RootView 시작하는 방법
stackoverflow.com/a/58575350/13065642
Could not find a storyboard named 'Main' in bundle
I'm getting a strange error: 'Could not find a storyboard named 'Main' in bundle NSBundle' when trying to run my app on a real iOS device. I have the file in my directory, and it works fine in the
stackoverflow.com
Navigation Controller를 RootView에 시작하는 방법
stackoverflow.com/a/28804024/13065642
Creating a navigationController programmatically (Swift)
I've been trying to redo the work on my app programmatically. (Without the use of storyboards) I'm almost done, except making the navigation controller manually. I've been doing some research but...
stackoverflow.com
'Xcode > IOS' 카테고리의 다른 글
IOS) Custom TabBar 만들기 in Code (0) | 2020.12.29 |
---|---|
IOS) Dynamic UIScrollView in Code (0) | 2020.12.25 |
IOS ) Share Extension에 대해 알아보자 (0) | 2020.12.20 |
IOS) TableVIew의 Pagination을 구현해보자 (0) | 2020.12.17 |
IOS) TableView의 Section을 다뤄보자 (0) | 2020.12.13 |
댓글