NSNotification

I wrote most of this post for StackOverflow since I’m getting multiple UIKeyboardDidShowNotification and UIKeyboardDidHideNotification notifications when I rotate the device and I can’t figure out why. But it has a good example of how to do notifications in iOS so I thought I’d post it here.

I have an app that lets you edit the text for each picture. (it’s in a UITextView.) Most of the time I need to slide the text up so you can see it above the keyboard, then I slide it down when you are done editing. Then I do a database update to save the new text. I use notifications to tell me when the keyboard is displayed and when it goes away. It works fine on iPad when the user taps the keyboard close icon on the keyboard. It also works fine if the user swipes to the next page and iOS closes the keyboard. Since iPhones and iPods don’t have a keyboard close key, I wrote a method to close the keyboard when the picture or background is tapped.

In my class that displays the pictures I start notifications when it is initialized.


- (id)initWithParentView:(UIView *)parentview  {
    
    self = [super init];
    if (self) {
        _parentView = parentview;
        if (ALLOW_DATABASE_EDITING) [self startNotifications];
    }
    
    return self;
}

- (void)startNotifications {
    
    // Listen for keyboard appearances and disappearances
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}

Note that the @selector() is the name of the method that is called when the notification center gets a notification.
The View Controller calls the hideKeyboard method in the View when the user taps on the picture.


- (void)dismissKeyboard {
    
    if (self.showArtic.keyBoardIsShowing) {
        [self.showArtic hideTheKeyboard];
    }
}
<code>

resignFirstResponder sends a notification that closes the keyboard. When it hides a notification is sent and the keyBoardDidHide method is called.
<code class='smaller'>
- (void)hideTheKeyboard {
    
    id <ShowArticDelegate> SA_delegate = _delegate;
    // Don't update the database when there is no text.
    if ( ![self.editableTextView.text isEqualToString:@""] ) {
        [SA_delegate updateTextInDatabase:self.editableTextView.text];
    }
    [self.editableTextView resignFirstResponder];
}

These methods respond to the notifications. I use block animations to move the text up and back. I also shade the background so it stands out a bit, but the picture is still visible under the text block. The self.keyBoardIsShowing flag is used in the view controller to decide whether to tell the keyboard to hide when the picture is tapped.


- (void)keyboardDidShow:(NSNotification *)notification {
    NSLog(@"keyboardDidShow called. Keyboard showing flag is %@.", self.keyBoardIsShowing ? @"YES" : @"NO");
    self.keyBoardIsShowing = YES;
    if (self.textShiftAmount > 0) {
        self.editableTextView.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:.5f];
        [self.parentView bringSubviewToFront:self.editableTextView];
        [UIView animateWithDuration:.7
                         animations:^{
                             
                             CGRect frame = self.editableTextView.frame;
                             frame.origin.y = self.pictButton.frame.origin.y + self.pictButton.frame.size.height - self.textShiftAmount;
                             self.editableTextView.frame = frame;
                             
                         }
                         completion:^(BOOL finished){
                         }];
    }
}

- (void)keyboardDidHide:(NSNotification *)notification {
    NSLog(@"keyboardDidHide called. Keyboard showing flag is %@.", self.keyBoardIsShowing ? @"YES" : @"NO");
    self.keyBoardIsShowing = NO;

    id <ShowArticDelegate> SA_delegate = _delegate;
    // Don't update the database when there is no text.
    if ( ![self.editableTextView.text isEqualToString:@""] ) {
        [SA_delegate updateTextInDatabase:self.editableTextView.text];
    }
    if (self.textShiftAmount > 0) {
        self.editableTextView.backgroundColor = [UIColor whiteColor];

        [UIView animateWithDuration:.7
                         animations:^{
                             
                             CGRect frame = self.editableTextView.frame;
                             frame.origin.y = self.pictButton.frame.origin.y + self.pictButton.frame.size.height;
                             self.editableTextView.frame = frame;
                             
                         }
                         completion:^(BOOL finished){
                             self.editableTextView.backgroundColor = [UIColor clearColor];
                         }];
    }
    
}

Programmatically move a segmented control.

I have a .xib file that I use for options. One of the options is a sound delay before I play a synthesized sound. Since synthesized sound is not available before iOS7, I figured that I’d hide the option. But that leaves a gap in the screen where the control used to be. This code moves the next segmented control up to where the original one was. Make sure you link the text label to the Files Owner by control clicking and dragging from the Files Owner to the label in Interface Builder section of Xcode.


if ( SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"7.0") ) {

    // Hide the delay control
    self.chooseDelaySegmentedControl.hidden = YES;
    self.targetSoundDelayText.text = @"";

    // Move the segmented control up
    CGRect scoringTypeFrame = self.chooseScoringTypeSegmentedControl.frame;
    scoringTypeFrame.origin.y = scoringTypeFrame.origin.y - 70.0f;
    self.chooseScoringTypeSegmentedControl.frame = scoringTypeFrame;
    
    CGRect scoringTypeTextFrame = self.scoringChoiceText.frame;
    scoringTypeTextFrame.origin.y = scoringTypeTextFrame.origin.y - 70.0f;
    self.scoringChoiceText.frame = scoringTypeTextFrame;
    
} 

Oddly enough, you can’t just change the x or y value, you need to change the whole frame.

Synthesize By Default

In 2012 Apple introduced Synthesize By Default in Xcode. You no longer have to synthesize instance variables. Just declare them as you normally would in the .h file and the compiler will automatically synthesize them for you. I learned how to use Xcode before the transition so I just learned about this by accident. If you haven’t done so, watch these two videos from WWDC 2012: Session 405 – Modern Objective-C and Session 413 – Migrating to Modern Objective-C.

Synthesize By Default is not available for NSManagedObjects so if your header file contains something like this:


@interface WordList : NSManagedObject

You’ll have to synthesize.

You can still manually create setters or getters and the compiler will automatically create the other accessor.

One caveat. I stopped supporting iOS4.3 and still have code that is compatible with iOS4. Since doesn’t have the weak indicator, you need to change assign to weak, then delete the ‘__unsafe_unretained id delegate;’ line. Then you can delete the @synthesize. If you still want to support iOS4, you’ll need to synthesize these variables.

Before


@implementation PracticeSightWords
@synthesize delegate = _delegate;


@interface PracticeSightWords : UIViewController {
    
    __unsafe_unretained id <PracticeSightWordsDelegate> delegate;
}

@property (nonatomic, assign) id <PracticeSightWordsDelegate> delegate;

After


@interface PracticeSightWords : UIViewController 

@property (nonatomic, weak) id <PracticeSightWordsDelegate> delegate;

I have my compiler warnings Turned up to 11 so I needed to turn off the warnings for “Implicit Synthesized Properties” in the ‘Warnings-Objective C’ section of the build settings.

New Icons and Launch Images for iOS7

iOS7 requires new images for the icons and launch image and Xcode now provides a new way of validating that you have all of the required images.

Just to see what Xcode would do, I created a new app from scratch and looked at the images it requires.

Launch Images

I then took a look at what an existing app looks like. The first thing I noticed is that I forgot to include the 100×100 iPad retina image. Then I noticed that the last four launch images are the same as the ones that I already have.

Launch Images

I manually added the Portrait Non-Retina and it automatically found the retina version. Then I added the Landscape Non-Retina and it found the retina. I looked in the .plist for the app and Xcode had added lines for the new files. Since I have over 20 apps I didn’t want to repeat this 20 or more times so I right-clicked on the file, opened the .plist as source, then copied the following lines.


<key>UILaunchImages~ipad</key>
  <array>
    <dict>
      <key>UILaunchImageMinimumOSVersion</key>
      <string>7.0</string>
      <key>UILaunchImageName</key>
      <string>Default-Landscape</string>
      <key>UILaunchImageOrientation</key>
      <string>Landscape</string>
      <key>UILaunchImageSize</key>
      <string>{768, 1024}</string>
    </dict>
    <dict>
      <key>UILaunchImageMinimumOSVersion</key>
      <string>7.0</string>
      <key>UILaunchImageName</key>
      <string>Default-Portrait</string>
      <key>UILaunchImageOrientation</key>
      <string>Portrait</string>
      <key>UILaunchImageSize</key>
      <string>{768, 1024}</string>
    </dict>
  </array>

I pasted these lines into the same place in each of the .plists.

While I was at it, I added a line to my pre-iOS7 images list for the file I forgot to include. That section now looks like this:


  <key>CFBundleIconFiles</key>
  <array>
    <string>Icon.png</string>
    <string>Icon@2x.png</string>
    <string>Icon-72.png</string>
    <string>Icon-72@2x.png</string>
    <string>Icon-Small-50.png</string>
                <string>Icon-Small-50@2x.png</string>
    <string>Icon-Small.png</string>
    <string>Icon-Small@2x.png</string>
  </array>

I then used Xcode to add the new images. (Note: As far as I can tell, you can name them anything you want. I followed a logical extension of the old rules.) I used the same image for Spotlight iPad Retina and iPhone Retina (80×80) so I name it Icon-402x.png.
Xcode added an iPad section to the .plist. The .plist now looks like this:


<key>CFBundleIconFiles</key>
  <array>
    <string>Icon-40</string>
    <string>Icon-iPhone-60</string>
    <string>Icon.png</string>
    <string>Icon@2x.png</string>
    <string>Icon-72.png</string>
    <string>Icon-72@2x.png</string>
    <string>Icon-Small-50.png</string>
    <string>Icon-Small-50@2x.png</string>
    <string>Icon-Small.png</string>
    <string>Icon-Small@2x.png</string>
  </array>
  <key>CFBundleIconFiles~ipad</key>
  <array>
    <string>Icon-40</string>
    <string>Icon-iPad-76</string>
    <string>Icon-iPhone-60</string>
    <string>Icon.png</string>
    <string>Icon@2x.png</string>
    <string>Icon-72.png</string>
    <string>Icon-72@2x.png</string>
    <string>Icon-Small-50.png</string>
    <string>Icon-Small-50@2x.png</string>
    <string>Icon-Small.png</string>
    <string>Icon-Small@2x.png</string>
  </array>

Xcode has a new folder type for Asset Catalogs. I did not convert my icons to asset catalogs, but it looks like a good way to manage assets.

Organizing the Rat’s Nest

In a previous post I mentioned that I needed OSX Mountain Lion for development and that I moved from using a laptop to a Mac mini. After using the setup for a while, I moved the rat’s nest of cables out of sight behind the desk.

I picked up a shelf that is close to the color of the desk and bolted it to the back. Then I drilled a few holes in it for cables mounted all of the dongles and cables on the board. This is what used to be on the top of the desk. There are two power strips, one that is visible in the picture and another under the drawer.

Desk Rat’s Nest

And this is what the desk looks like now.

Desk

There is only one cable that the bird can chew and no power cords, so we’re both happy—he’s happy that he can help me type and I’m happy that he can’t electrocute us.