Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEC-492/693/793 iPhone Application Development

Similar presentations


Presentation on theme: "EEC-492/693/793 iPhone Application Development"— Presentation transcript:

1 EEC-492/693/793 iPhone Application Development
Lecture 9 Wenbing Zhao & Nigamanth Sridhar 9/21/2018 EEC492/693/793 - iPhone Application Development

2 Outline Administrative Objective-C
This Wed. 10/6: make-up session Students who have completed all assignments can omit it Oct 11 (next Monday), Columbus day, no class Objective-C @selector Tab bar based multiview applications Pickers Assignments: Build the Pickers app (chapter 7) Continue building the calculator app Objective-C part is shamelessly taken from Stanford CS193P winter 2010 lecture 3 9/21/2018 9/21/2018 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2

3 EEC492/693/793 - iPhone Application Development
@selector @selector is somewhat similar to the function pointer in C, or more accurately, the dlsym() call It offers a mechanism for a developer to dynamically choose a method to invoke (or a message to send in OO language) @selector takes the name of a method (with all arguments stripped off) @selector returns a special type, SEL SEL setWidthHeight; setWidthHeight // assuming we have a method: -(void)setWidth: (int)w height: (int)h 9/21/2018 EEC492/693/793 - iPhone Application Development

4 EEC492/693/793 - iPhone Application Development
@selector You can call a method (or send a message) If you have a set of methods defined like these: - (void) doSomething: - (void) doSomething:(NSObject*)obj - (void) doSomething:(NSObject*)obj1 withObject:(NSNumber*)obj2 The selectors will be declared like these, respectively: @selector(doSomething:) @selector(doSoemthing:) //same as before @selector(doSomething:withObject:) To perform a selector on an object, you can use: [someObj [someObj withObject: o]; [someObj withObject: o1, withObject: o2]; 9/21/2018 EEC492/693/793 - iPhone Application Development

5 EEC492/693/793 - iPhone Application Development
@selector Want to check if an object will respond to a message obtained via a selector? -(BOOL)respondsToSelector:(SEL)aSelector if ( [anObject ) [anObject setOrigin:0.0 :0.0]; else fprintf(stderr, "%s can’t be placed\n", [NSStringFromClass([anObject class]) UTF8String]); Q: Why double colons in setOrigin:: ? 9/21/2018 EEC492/693/793 - iPhone Application Development

6 EEC492/693/793 - iPhone Application Development
@selector @selector used in chapter 7 NSArray *sorted = [components [self withObject:nil afterDelay:1.5]; [self withObject:nil afterDelay:.5]; 9/21/2018 EEC492/693/793 - iPhone Application Development

7 Tab Bar Based Multiview Applications
Create needed individual view controllers and their nib files Modify the app delegate: Use UITabVarController as the root view controller Add an outlet to the root controller Add the root view as a subview to the main window [window addSubview:rootController.view]; Connect the outlet to the root controller Control-drag from the app delegate icon to the Tab Bar Controller icon, selecting the rootController outlet Tab Bar Controller View Controller 9/21/2018 EEC492/693/793 - iPhone Application Development

8 Tab Bar Based Multiview Applications
In the IB, drag a Tab Bar Controller from the library to MainWindow.xib Modify the number of views needed in the attribute inspector of the tab var controller (use the + or – sign) Highlight each tab, link with nib file (⌘1), and specify to the proper controller class (⌘4) Click on the tab again to change the selection to the tab bar item. In the inspector, link with the image file, and change to the desirable title Implementing individual content views 9/21/2018 EEC492/693/793 - iPhone Application Development

9 EEC492/693/793 - iPhone Application Development
Pickers A picker lets the user select an item from a list For the picker to work, you must provide it with both a picker delegate and a picker datasource The picker defers several jobs to its delegate Determine what to draw for each of the rows in each of its components The picker asks the delegate for either a string or a view that will be drawn at a given spot on a given component The picker also gets its data from the delegate The datasource tells the picker how many components it will be working with and how many rows make up each component For a simple picker, its view controller is usually used as the datasource and delegate 9/21/2018 EEC492/693/793 - iPhone Application Development

10 Pickers: Datasource and Delegate
@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> { UIPickerView *singlePicker; NSArray *pickerData; } #pragma mark - #pragma mark Picker Data Source Methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1;} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [pickerData count]; } #pragma mark Picker Delegate Methods - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [pickerData objectAtIndex:row]; 9/21/2018 EEC492/693/793 - iPhone Application Development

11 Pickers: More Delegate Methods
The content of one component might have to be updated if a row has been selected in a different component (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == kStateComponent) { NSString *selectedState = [self.states objectAtIndex:row]; NSArray *array = [stateZips objectForKey:selectedState]; self.zips = array; // swap zips array whenever the state changes // set the right-hand component back to the 1st row [picker selectRow:0 inComponent:kZipComponent animated:YES]; // tell it to reload itself [picker reloadComponent:kZipComponent]; } 9/21/2018 EEC492/693/793 - iPhone Application Development

12 Pickers: More Delegate Methods
The width of each component in a picker can be adjusted by implementing a delegate method - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (component == kZipComponent) return 90; return 200; } 9/21/2018 EEC492/693/793 - iPhone Application Development

13 EEC492/693/793 - iPhone Application Development
NSBundle A bundle is a special type of folder whose contents follow a specific structure Applications and frameworks are both bundles A primary use of NSBundle is to get to resources that added to the Resources folder of the project If we want to get to those resources in our code, we usually have to use NSBundle NSBundle *bundle = [NSBundle mainBundle]; // this will return a string containing the location of the statedictionary.plist file NSString *plistPath = [bundle // use the plist NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 9/21/2018 EEC492/693/793 - iPhone Application Development

14 UIImage and UIImageView
How to create a UIImageView // by using a convenience method on the UIImage class, we don’t have to // first determine the location of each image and then use that info to // load the image UIImage *seven = [UIImage UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven]; 9/21/2018 EEC492/693/793 - iPhone Application Development

15 More Methods on Property
Set a property based on its name (mutator) setValue:forKey: [self setValue:imageViewArray forKey:fieldName]; // If fieldName is column1, and we have a property called // column1, it is equivalent to: [self setColumn1 imageViewArray) Get the value of a property based on its name (accessor) valueForKey: NSArray *array = [self valueForKey:arrayName]; 9/21/2018 EEC492/693/793 - iPhone Application Development

16 Adding Sounds to Your App
Import header file: #import <AudioToolbox/AudioToolbox.h> Getting the path to the sound file NSString *path = [[NSBundle mainBundle] Create sound ID SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID); Play the sound AudioServicesPlaySystemSound (soundID); You must manually link with the AudioToolBox.framework Make sure select a Reference Type of Relative to Current SDK 9/21/2018 EEC492/693/793 - iPhone Application Development


Download ppt "EEC-492/693/793 iPhone Application Development"

Similar presentations


Ads by Google