my favourite 10 things about xcode/objectivec

21
My Favourite Ten Xcode Things Tuesday, October 11, 11 Some of my favourite Xcode and Objective-C type things.

Upload: johnkennedy

Post on 09-May-2015

1.352 views

Category:

Education


0 download

DESCRIPTION

A quick look at some of the features I enjoy when developing for iOS.

TRANSCRIPT

Page 1: My Favourite 10 Things about Xcode/ObjectiveC

My Favourite Ten Xcode Things

Tuesday, October 11, 11

Some of my favourite Xcode and Objective-C type things.

Page 2: My Favourite 10 Things about Xcode/ObjectiveC

(Or: Please tell me a better way of doing these things)

Tuesday, October 11, 11

Three phases of iPhone development:

1. Argh! Square brackets!2. Ok, how do I do this?3. Ok, what’s the BEST way to do this?

The common theme is that each stage is full of puzzles, and puzzles are attractive to certain types of brains. You think, you solve, you get a little rush. Addictive!

Page 3: My Favourite 10 Things about Xcode/ObjectiveC

Flip between .h and .m

Control + Command + Arrow up

Switch between open tabs

Shift + Command + [ or ]

Find anywhere in project

Shift + Command + F

1. Xcode Keyboard Shortcuts

Tuesday, October 11, 11

I spend many hours using Xcode every day. The transition to Xcode 4 wasn’t too bad, although it took a while to get used to the way it splits views. I’m happy enough now though. At least it’s not Eclipse, and when I go back to Visual Studio it irritates me that I need to double-click on things a lot.

Page 4: My Favourite 10 Things about Xcode/ObjectiveC

Toggle Debug Console

Shift + Command + Y

Open a file

Shift + Command + O

1b. Xcode Keyboard Shortcuts

Tuesday, October 11, 11

Open a file is fantastic. Use it.

Page 5: My Favourite 10 Things about Xcode/ObjectiveC

Hold down Control and drag a control into your .h file, to create actions and properties (and the associated methods in .m).

2. Drag from the NIB editor into .h files

Tuesday, October 11, 11

I love this.

Page 6: My Favourite 10 Things about Xcode/ObjectiveC

-(NSString *)getAxis{! return [NSString stringWithFormat: @"X:%0.2f Y:%0.2f Z:%0.2f", _accelerometer[0], _accelerometer[1], _accelerometer[2]];}

3. Useful NSString initialization

Tuesday, October 11, 11

Very nice - and because there is no ALLOC, it’s an autorelease object.Just don’t put a BOOL in there.

Page 7: My Favourite 10 Things about Xcode/ObjectiveC

BOOL myStatus = TRUE;

-(NSString *)getStatus{

! return [NSString stringWithFormat: @"%@",(myStatus ? @”YES” : @”NO”)]; }

3b. Useful NSString initialization

Tuesday, October 11, 11

Handy for NSLog too.

Page 8: My Favourite 10 Things about Xcode/ObjectiveC

Don’t check to see if a string is empty like this:

if (myString == nil) { NSLog(@"empty!"); }

Use this:

if ([myString isEqualToString:@""]) { NSLog(@"empty!"); }

Or this: if ([myString length] == 0) { NSLog(@"empty!"); }

4. Useful NSString comparisons

Tuesday, October 11, 11

But if it’s just whitespace, that might not work..

Page 9: My Favourite 10 Things about Xcode/ObjectiveC

//  Mr.  Will  Shipleystatic  inline  BOOL  isEmpty(id  thing)  {        return  thing  ==  nil        ||  [thing  isKindOfClass:[NSNull  class]]        ||  ([thing  respondsToSelector:@selector(length)]                &&  [(NSData  *)thing  length]  ==  0)        ||  ([thing  respondsToSelector:@selector(count)]                &&  [(NSArray  *)thing  count]  ==  0);}

4b. Useful NSString comparisons

Tuesday, October 11, 11

Here’s the 100% best way.

Page 10: My Favourite 10 Things about Xcode/ObjectiveC

There is no file system. Except there is.

if ([[NSFileManager defaultManager] fileExistsAtPath:[[NSBundle mainBundle] pathForResource:@"picture" ofType:@"jpg"]]) return YES;

5. Testing to see if a file exists

Tuesday, October 11, 11

Also, are you using large image files? Tempted to stick with PNG? Try JPG instead.Same quality in photograph images, but can be 1/10th of the size.

Page 11: My Favourite 10 Things about Xcode/ObjectiveC

-(void) myMethod{ }

[self performSelector:@selector(myMethod) withObject: nil afterDelay: 1.0];

[NSObject cancelPreviousPerformRequestsWithTarget :self selector :@selector(myMethod) object :nil];

6. Time delay before method call

Tuesday, October 11, 11

Multithreading for dummies - a great way to get animation working when the main thread would otherwise be blocked. WARNING! Remember to cancel it if you don’t use it before the reference goes away!

Page 12: My Favourite 10 Things about Xcode/ObjectiveC

// In AppDelegate-(void)callMe{ }

// In some other class- (void)viewDidLoad{ [super viewDidLoad]; [(AppDelegate*) [[UIApplication sharedApplication] delegate] callMe]; }

7. Delegate callback to parent AppDelegate

Tuesday, October 11, 11

Just need to add callMe to .h in AppDelegate, and #import that into the other class.However, think about why you are doing this: it’s very likely that a Singleton will be a better idea.

Page 13: My Favourite 10 Things about Xcode/ObjectiveC

// In the Modal Dialog UIViewController Class, .h file

@interface CityPickerViewController : UIViewController <UIPickerViewDelegate>

{!! id delegate;!}

- (id)delegate;- (void)setDelegate:(id)newDelegate;

@end

8. Using delegates to perform actions when modal dialog closed.

Tuesday, October 11, 11

So you want to trigger something when a modal dialog is closed. Use this to allow the Modal dialog UIViewController class to call a method back in the class that called it.

Page 14: My Favourite 10 Things about Xcode/ObjectiveC

// In the Modal Dialog UIViewController Class, .m file

- (id)delegate { return delegate;}

- (void)setDelegate:(id)newDelegate { delegate = newDelegate;}

-(void) updateCity: (id)sender{! // replaced by delegated call!}

-(void)callWhenDialogClosing{! [[self delegate] updateCity:self];!}

8b. Using delegates to perform actions when modal dialog closed.

Tuesday, October 11, 11

So you want to trigger something when a modal dialog is closed. Use this to allow the Modal dialog UIViewController class to call a method back in the class that called it.

Page 15: My Favourite 10 Things about Xcode/ObjectiveC

// In the class that is calling the modal dialog

-(void) updateCity: (id)sender{! [self drawLocation];! [myPickerView dismissModalViewControllerAnimated:YES];}

-(IBAction) clickNearestCity: (id)sender{ myPickerView = [[CityPickerViewController alloc] initWithNibName:@"CityPickerViewController" bundle:nil];

! myPickerView.delegate = self; ....

8b. Using delegates to perform actions when modal dialog closed.

Tuesday, October 11, 11

You can’t keep the pointer to the view nice and local now, as you’ll need to close it yourself in the delegate. Yes, it smells a bit. Suggestions welcome.

Page 16: My Favourite 10 Things about Xcode/ObjectiveC

[UIView beginAnimations:@"clickBounce" context:tempButton];

[UIView setAnimationDelegate:self];

[UIView setAnimationDuration:0.2]; !

[UIView setAnimationDidStopSelector:@selector(part2:finished:context:)];

[myButton setTransform:CGAffineTransformMakeScale(.9,.9)];

[UIView commitAnimations];

9. Two-part animation block

Tuesday, October 11, 11

Blocks have a terribly ugly syntax, but if you can get over it, they are useful. And increasing in importance. If you wanted an animation to have multiple parts, you needed to use the DidStopSelector, and it was a pain.

Page 17: My Favourite 10 Things about Xcode/ObjectiveC

[UIView animateWithDuration:1.0 animations:^ { myButton.alpha = 1.0; myButton.transform = CGAffineTransformMakeScale(1.5, 1.5); } completion:^(BOOL completed) { myButton.alpha = 0.8; myButton.transform = CGAffineTransformMakeScale(1, 1); }];

9b. Two-part animation block

Tuesday, October 11, 11

Much neater, no extra methods, easier to pass values between different components of the animation etc etc

Page 18: My Favourite 10 Things about Xcode/ObjectiveC

Declare a Collection, and then link all your buttons to it..

IBOutletCollection(UIButton) NSArray *menuButtonsCollection;

10. UICollections, Tags and iOS 5

Tuesday, October 11, 11

Piece of cake in NIB editor.

Page 19: My Favourite 10 Things about Xcode/ObjectiveC

Now you can iterate over them all..

for (UIButton *button in menuButtonsCollection) { [button setImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal]; }

10b. UICollections, Tags and iOS 5

Tuesday, October 11, 11

Handy to programmatically fade out all buttons for example.Remember you can also set TAGS in the NIB editor, and then act on those in the loop.You don’t need to only use one type of object, use (id) for any control.

Page 20: My Favourite 10 Things about Xcode/ObjectiveC

If you need to change them all at once..

[[UISwitch appearance] setOnTintColor: [UIColor redColor]];

10b. UICollections, Tags and iOS 5

Tuesday, October 11, 11

Note: need to re-open the view to see the changes.