Presentation is loading. Please wait.

Presentation is loading. Please wait.

Designing with Introspection

Similar presentations


Presentation on theme: "Designing with Introspection"— Presentation transcript:

1 Designing with Introspection
iPhone/iPad, iOS Development Tutorial

2 Dynamic Binding Objective – C allows dynamic binding: a feature that makes it possible to postpone decisions about what classes we are dealing until runtime. In order to make this kind of decision however, we need to be able to determine exactly what class or classes we are dealing with.

3 Open a new “Single View” app
Start Xcode, choose “Create a new Xcode project,” select the Single View Application template, and click Next. Select “Use Auto Reference Counting” Don’t select the storyboard. Don’t really need it for this application. Select ‘iPhone’ as the app type

4 Create the Interface Drag a “View” from the library to the window.
Put eight labels in the view. You should have a large white square which is a UIView object containing eight labels. All other controls are on the main view.

5 Create the interface

6 Add items to ViewController.h
Open ViewController.h file, and make changes as shown below: (IBAction)test1Pressed:(UIButton *)sender; (IBAction)test2Pressed:(UIButton *)sender (IBAction)test3Pressed:(UIButton *)sender; (IBAction)test4Pressed:(UIButton *)sender;

7 ViewController.h We are only declaring action methods for the four UIButton objects. We don’t need IBOutlets in this app; We are going to find controls in the view by inspecting the properties of the objects inside the main view using introspection.

8 - (IBAction)test1Pressed:(UIButton. )sender { for (UIView
- (IBAction)test1Pressed:(UIButton *)sender {     for (UIView *aView in self.view.subviews) {         if ([aView isMemberOfClass:[UILabel class]]) {             aView.backgroundColor = [UIColor redColor];         }     } } - (IBAction)test2Pressed:(UIButton *)sender {     for (UIView *aView in self.view.subviews) {         if ([aView isKindOfClass:[UIView class]]) {             aView.backgroundColor = [UIColor blueColor];         }     } } - (IBAction)test3Pressed:(UIButton *)sender {     for (id aView in self.view.subviews) {         if ([aView withSender:sender]) {             [aView         }     } } - (IBAction)test4Pressed:(UIButton *)sender {     for (UIView *aView in self.view.subviews) {         if ([aView isMemberOfClass:[UIView class]]) {             for (UILabel *aLabel in aView.subviews) {                 aLabel.backgroundColor = [UIColor greenColor];             }         }     } }

9 ViewController.m Add the methods immediately after line in the .m file. Run the application and observe the results of pressing each button.

10 What are we doing? The first test looks at all subviews of the main view. It then uses isMemberOfClass on each one to determine if each view is a UILabel. The isMemberOfClass method returns YES if an object is an instance of one specific class. So if this method finds a UILabel that is a subview of the main view, it will set its background color to red:

11 Test1 Button

12 What are we doing? The label objects inside the white square UIView do not get their background color set to red. This is because these labels are subviews of a subview, not direct subviews of the main view. The second test looks for all subviews of the main view that are descended from UIView. This is accomplished by calling isKindOfClass. Since all controls are descendents of UIView, this method will change the background color of all of the subviews to blue:

13 Test2 Button

14 What did we do? Again, the background color of the labels internal to the white square view is unchanged, since the default background color of a UILabel is transparent. Test 3 searches all the subviews of the main view for objects that have a setText method. It accomplishes this by using the canPerformAction method. Since the only subview of the main view that has a setText: method is the UILabel at the top, the method changes the text to “Hello!”:

15 Test3 Button

16 What did we do? Test 4 shows one way to get to the label objects in the white square view. First, we must find a subview of the main view that is a member of the UIView class. Then, we look for subviews of that view that are UILabels, and set their background color to green:

17 Test4 Button

18 Summary Introspection is useful when we have a large number of similar controls and we want to find a particular control by looking at some attribute (for example, a tag). It’s also useful when we’re using inheritance. We might want all subclasses of some class to perform a method, but only if that method is defined. Since we might not know what objects will be performing the method at compile time, we can see that introspection is an extremely powerful tool.


Download ppt "Designing with Introspection"

Similar presentations


Ads by Google