1. Trang chủ
  2. » Công Nghệ Thông Tin

iOS 5 Programming Cookbook phần 5 pptx

89 331 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Figure 4-13. Selecting the navigation controller in Interface Builder Figure 4-14. Selecting a navigation controller as the initial view controller of a storyboard As you can see, now your navigation controller has a border around it. Now if you run your application, you will notice that the initial view controller has a navigation bar on 340 | Chapter 4: Storyboards top, indicating that this view controller now has a navigation controller. In the next recipes we will see how we can make use of the navigation controller to display new scenes on the screen. Figure 4-15. The navigation bar on a view controller created with a storyboard We now have a navigation controller with a view controller inside it but our objective now is to trigger an action and then move from one view controller to another. Alright then; let's place a button on our view controller and push a view controller into the stack once the user presses the button. Sounds good? Alright, let's get started: 4.2 Adding a Navigation Controller to a Storyboard | 341 1. Go back to your .storyboard file. 2. In the Object Library, find the View Controller object (see Figure 4-16) and drag and drop it onto the storyboard, on the right side of our existing view controller, as shown in Figure 4-17. Figure 4-16. A view controller object in the Object Library 342 | Chapter 4: Storyboards Figure 4-17. Adding a new view controller to a storyboard 3. In Object Library, find the Button object (see Figure 4-18) and drag and drop it into the first view controller (see Figure 4-19). Note that if you are zoomed out, Interface Builder will not allow you to drop a button onto a view controller. You need to double click on an empty space on your storyboard to zoom into it before Interface Builder allows you to drop UI components onto your view controllers. Figure 4-18. Selecting the Button object in the Object Library 4.2 Adding a Navigation Controller to a Storyboard | 343 Figure 4-19. Dropping a button on the first view controller in our storyboard 4. Now select the button, hold down the Control key on your keyboard and then hold down the left mouse button and on the button and drag all the way to the second view controller (see Figure 4-20. 344 | Chapter 4: Storyboards Figure 4-20. Connecting a button to another view controller in a storyboard 5. Now lift your fingers off the mouse button and the Control key on your keyboard. You will now be presented with a dialog similar to that shown in Figure 4-21. Simply click on the performSegueWithIdentifier:sender: item. Figure 4-21. Making a button perform a segue Now if you have a look at your storyboard, you will see that the first view controller is connected to the second view controller, as shown in Figure 4-22: 4.2 Adding a Navigation Controller to a Storyboard | 345 Figure 4-22. The first view controller is connected to the second view controller through a segue Now if you run your app and tap on the button on the first view controller, you'll see that the second view controller will automatically get pushed onto the stack of view controllers. Once the second view controller is presented, you will see a back button on the navigation bar. If you press that button, you will be sent back to the first view controller. See Also XXX 4.3 Passing Data From One Screen to Another Problem You want to pass data from one scene to another using storyboards. Solution Use segue objects. 346 | Chapter 4: Storyboards Discussion A segue is an object, just like any other object in Objective-C. When a trasition is happening from one scene to another, the storyboard runtime creates a segue object for that transition. A segue is an instance of class UIStoryboardSegue. When a transition happens, the current view controller (that will get pushed out of the screen after the segue) will receive the prepareForSegue:sender: message where the prepareForSegue parameter will be an object of type UIStoryboardSegue. If you want to pass any data from the current view controller to view controller which is about to appear on the screen, you need to do that in the prepareForSegue:sender: method. For this recipe to make sense, you need to have followed the instructions in Recipe 4.2 and created two view controllers inside a navigation con- troller on your storyboard. Let's implement the prepareForSegue:sender: method in the first view controller: - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ NSLog(@"Source Controller = %@", [segue sourceViewController]); NSLog(@"Destination Controller = %@", [segue destinationViewController]); NSLog(@"Segue Identifier = %@", [segue identifier]); } If you run this app now, you will see the results in the console window. However, if you noted, the identifier is nil. Each segue has an identifier which can uniquely identify that segue. Since one scene can have more than one segue associated with it, it is good to give your segues identifiers that you can then detect in your view controllers and take action accordingly. A segue object in Interface Builder is the connection between two scenes. Here is the segue between my first view controller and the second: 4.3 Passing Data From One Screen to Another | 347 Figure 4-23. Selecting a segue object in Interface Builder Follow these steps to give your segue an identifier: 1. Select your segue object in Interface Builder by clicking on it. 2. From the View menu, select Utilities→Show Attributes Inspector. 3. In the Attributes Inspector, in the Identifier text field, simply write the identifier that you would like this segue to carry with itself. You can see that when the storyboard runtime calls the prepareForSegue:sender: meth- od in the current view controller to prepare it for the segue, the destination view con- troller has already been initialized in the segue object. Now this is your chance to pass any required data to the destination view controller. You can either set the data directly into a property of the destination view controller or pass your data by calling a method on that view controller; it is really up to you. In this code, my second view controller is of clas SecondViewController and I've given my segue the identifier of SimpleSegueTo SecondViewController: - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ NSLog(@"Source Controller = %@", [segue sourceViewController]); NSLog(@"Destination Controller = %@", [segue destinationViewController]); NSLog(@"Segue Identifier = %@", [segue identifier]); if ([[segue identifier] isEqualToString:@"SimpleSegueToSecondViewController"]){ SecondViewController *viewController = [segue destinationViewController]; viewController.dataModel = 348 | Chapter 4: Storyboards } } See Also XXX 4.4 Adding a Storyboard to an Existing Project Problem You have already coded your app without storyboards and now you would like to start using storyboards instead handling the flow of your app manually. Solution Simply follow these steps to allow your non-storyboard apps to take advantage of storyboards: 1. From the File menu, choose New→New File 2. In the New File dialog, make sure you have selected the Resource sub-category of the iOS category on the left and then choose Storyboard item on the right and press Next (see Figure 4-24. 4.4 Adding a Storyboard to an Existing Project | 349 [...]... learn about formatting strings with system-independent format specifiers in Objective-C, please refer to String Programming Guide, iOS Developer Library on Apple’s website The block object equivalent of this C function is shown in Example 5- 1 5. 1 Constructing Block Objects | 359 Example 5- 1 Example block object defined as function NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger){ NSString... task) but will not block the main thread 5. 1 Constructing Block Objects Problem You want to be able to write your own block objects or use block objects with iOS SDK classes Solution You just need to understand the basic differences between the syntax of block objects and classic C functions These differences are explained in the Discussion section 358 | Chapter 5:  Concurrency Discussion Block objects... you Block objects in Objective-C are what the programming field calls first-class objects This means you can build code dynamically, pass a block object to a method as a parameter, and return a block object from a method All of these things make it easier to choose what you want to do at runtime and change the activity of a program In 5. 0 Introduction | 355 particular, block objects can be run in individual... objects 368 | Chapter 5:  Concurrency See Also XXX 5. 3 Invoking Block Objects Problem You've learned how to construct block objects and now you want to execute your block objects to get results Solution Execute your block objects the same way you execute a C function, as shown in the Discussion section Discussion We’ve seen examples of invoking block objects in Recipe 5. 1 and Recipe 5. 2 This section contains... instances, you must make sure the block object submitted to GCD has access in its scope to all the values that it requires Running this app in iOS Simulator, the user will get results similar to those shown in Figure 5- 1 372 | Chapter 5:  Concurrency Figure 5- 1 An alert displayed using asynchronous GCD calls This might not be that impressive In fact, it is not impressive at all if you think about it... doTheConversion method (Example 5- 3) Example 5- 3 Example block object defined as function - (void) doTheConversion{ IntToStringConverter inlineConverter = ^(NSUInteger paramInteger){ NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger]; return result; }; NSString *result = [self convertIntToString:123 usingBlockObject:inlineConverter]; 5. 1 Constructing Block Objects |... dispatch_async(queue, ^(void) { dispatch_apply(numberOfIterations, queue, ^(size_t iteration){ /* Perform the operation here */ 354 | Chapter 5:  Concurrency }); }); In this chapter, you will learn all there is to know about GCD and how to use it to write modern multithreaded apps for iOS and Mac OS X that will achieve blazing performance on multicore devices such as the iPad 2 We will be working with dispatch... is to simply leave this method as it is but comment out any lines that might be changing the window's root view controller object See Also 4.4 Adding a Storyboard to an Existing Project | 351 CHAPTER 5 Concurrency 5. 0 Introduction Concurrency is achieved when two or more tasks are executed at the same time Modern operating systems have the ability to run tasks concurrently even on one CPU They achieve... to update your UI, so you can imagine how important it is 5. 5 Performing UI-Related Tasks with GCD | 373 Instead of submitting a block object for execution on the main queue, you can submit a C function object Submit all UI-related C functions for execution in GCD to the dispatch_async_f function We can get the same results as we got in Figure 5- 1, using C functions instead of block objects, with a few... output of this code would be similar to that shown here: Current thread = {name = (null), num = 1} Main thread = {name = (null), num = 1} 5. 5 Performing UI-Related Tasks with GCD | 3 75 . refer to String Programming Guide, iOS Developer Library on Apple’s website. The block object equivalent of this C function is shown in Example 5- 1. 5. 1 Constructing Block Objects | 359 . operation here */ 354 | Chapter 5:  Concurrency }); }); In this chapter, you will learn all there is to know about GCD and how to use it to write modern multithreaded apps for iOS and Mac OS X. easier to choose what you want to do at runtime and change the activity of a program. In 5. 0 Introduction | 355 particular, block objects can be run in individual threads by GCD. Being Objective-C objects,

Ngày đăng: 13/08/2014, 18:20

Xem thêm: iOS 5 Programming Cookbook phần 5 pptx

TỪ KHÓA LIÊN QUAN

Mục lục

    4.2€ Adding a Navigation Controller to a Storyboard

    4.3€ Passing Data From One Screen to Another

    4.4€ Adding a Storyboard to an Existing Project

    5.2€ Accessing Variables in Block Objects

    5.4€ Dispatching Tasks to Grand Central Dispatch

    5.5€ Performing UI-Related Tasks with GCD

    5.6€ Performing Non-UI-Related Tasks Synchronously with GCD

    5.7€ Performing Non-UI Related Tasks Asynchronously with GCD

    5.8€ Performing Tasks After a Delay with GCD

    5.9€ Performing a Task at Most Once with GCD

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w