본문 바로가기
Programming/ios

[swift를 이용한 ios 앱 만들기] 화면 전환

by guru_k 2015. 12. 30.
728x90
반응형

버튼을 클릭 시 새로운 화면으로 전환되는 이벤트를 실행합니다.




1. AppDelegate.swift에 메인 페이지인 ViewController를 rootViewController로 설정합니다.


AppDelegate.swift

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    var window: UIWindow?
    var naviController : UINavigationController?
 
    func application(application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.naviController = UINavigationController(rootViewController: ViewController())
        self.naviController?.navigationBar.hidden = true
        self.window?.rootViewController = self.naviController
        self.window?.makeKeyAndVisible()
        
        return true
    }
}
cs


2. 페이지가 전환될 새로운 Controller를 생성합니다. 저는 NewController.swift로 생성하였습니다. 새로운 페이지를 위한 백그라운드 색상과 텍스트를 변경하였습니다.


NewController.swift

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import UIKit
 
class NewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // BackGround Color -- R, G, B, Alpha
        self.view.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.3)
        
        
        //------------
        // TEXT
        
        // Get width, height Size
        var width : CGFloat = self.view.frame.size.width
        var height : CGFloat = self.view.frame.size.height
        
        // Title UILabel
        var titleTxt = UILabel(frame: CGRectMake(00, width, height/6))
        // Text
        titleTxt.text = "New Page"
        // Alignment
        titleTxt.textAlignment = NSTextAlignment.Center
        // Font
        titleTxt.font = UIFont(name: "System", size: 40)
        // Color
        titleTxt.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        
        // Display
        self.view.addSubview(titleTxt)
      
        //------------------   
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
cs



3. 앞서 생성했던 버튼 이벤트에 새로운 Controller로 변경하는 부분을 추가해줍니다.


ViewController.swift

 

1
2
3
4
5
6
     // btn ClickEvent
    func touchBtn(sender : UIButton){
        //새로운 Controller로 뷰 이동
        self.navigationController?.pushViewController(NewController(), 
animated: false)
 
    }
cs
 


출력 화면







728x90
반응형

댓글