Collection By traibingo
33
Bài 5: Transitioning Between Views
Phần này sẽ hướng dẫn cho các bạn cách chuyển qua lại giữa các UIView
với nhau. Có 2 cách để chuyển qua lại giữa các View: 1. Nếu sử dụng
NavigationBar thì đơn giản là push view cần chuyển vào NavigationBar vì
thực tế NavigationBar là một stack chứa các View. Đối tượng View được
hiển thị là đối tượng ở trên đỉnh stack. Ngược lại để quay lại View trước đó
thì chỉ cần pop View trên cùng ra. Cách làm chi tiết như thế nào thì bài này
sẽ từng bước hướng dẫn cụ thể cho các bạn. 2. Nếu không sử dụng
NavigationBar và bạn muốn chuyển từ một View hiện tại sang một View
khác thì bản thân đối tượng View đã cung cấp cho chúng ta hàm
addSubView. Nếu bạn đang đứng ở một đối tượng kế thừa từ UIView thì
sử dụng lệnh [self addSubView:<View Object need to switch>] Dưới đây là
nguyên bản tutorial trong trang
This tutorial will focus on transitioning from one view to another. We will be
utilizing Apple‟s UINavigationController. I will be using the code from the
“Hello World” tutorial that I previously wrote. So if you have not completed it
yet, go ahead and do it and come back to this page. (It‟s quick I promise).
You can view it here.
In this tutorial you will learn:
Add A New View
Add A View Controller
Set Up The Transition To The View
Connect The View To The Code
Add A Back Button
The first thing we are going to do is change our “Hello World” text to say
something that sounds more like navigation. Go ahead and open
36
Next, you will see a screen asking you if you want to add the View to your
project. Check the box next to Hello World and click Add.
Close Interface Builder. Drag the View2.xib file into the Resources
folder, if it didn‟t appear there by default (this will help maintain
organization).
Collection By traibingo
37
Add A View Controller
Now we need to create a ViewController class. This class will be used to
connect the view that we just created to our code. Inside of Xcode click
File -> New File… Select UIViewController subclass and click Next.
Name it View2ViewController and make sure “Also create
“View2ViewController.h” “ is checked. Click Finish to continue. This will
add the new ViewController to your project.
Collection By traibingo
38
For organization sake, drag your View2ViewController.h and .m files into
the Classes folder if they didn‟t appear there to begin with.
Set Up The Transition To The New View
Open up RootViewController.h and add the following code:
First we check to see if self.view2ViewController is null. This will happen
the first time the user presses on the table cell. After this, the
viewController gets stored in memory to optimize performance. Next we
create a new instance of a View2ViewController and set it to our
view2ViewController. Remember to pay attention to a capitol “V” verses a
lowercase “v”. After we set this viewController to our viewController, it
Collection By traibingo
40
should be released. Remember, objective-c does not have a garbage
collector, so we need to clear all of our unused objects from memory.
Finally, the last line of code is what actually transitions our view to the
newly created view. The navigationController object is a stack that
contains viewControllers. The view at the top of the stack is the one that
gets rendered. So all we are doing is pushing a viewController onto this
stack. There last part animated:YES, tells the compiler that we want an
animated transition to the next view.
Connect The View To Code
Before this code will execute, we must connect the code that we just wrote
to the view we just created. Double click on View2.xib to open it up in
Interface Builder. We need to associate our View2ViewController class
object with the view so click on the File’s Owner object and then click
Tools -> Identity Inspector.
Click the dropdown next to class and select View2ViewController.
Collection By traibingo
41
44
When you click on “Next View” it should transition to:
Collection By traibingo
45
Notice the back button at the top with the text “Hello”. If you press it, your
view will be popped from the NavigationController stack and the previous
view will be shown. If you have any problems/questions/comments post
them in the comments. I‟m pretty good about answering them as it emails
me when you do so and I receive them on my iPhone. If you have any
problems, you can download the source code here Hello World Views
Source. Happy iCoding!