Removing subviews from a view.

In my apps, I use swipe gestures to move to the next screen. The old stuff is animated off to the left and the new stuff animates in from the right. If the user rotates the device, I need to remove the elements of the view from the main view and redisplay them in the new orientation. I also need to remove the animations if they are occurring. I have lots of different layouts, but each layout has this method in it. (The contents vary a bit.)

I first remove the animations. Then I remove the views from the main view. Then I set the views to nil to make sure their resources are released.


- (void)removeAllObjectsFromParentView {
    [self.wordView.layer removeAllAnimations];
    [self.categoryView.layer removeAllAnimations];
    
    [self.wordView removeFromSuperview];
    [self.categoryView removeFromSuperview];
   
    self.wordView = nil;
    self.categoryView = nil;
}

All of the subviews of wordView and categoryView will be removed and released when the parent view is removed and released.

Pick a random color in iOS

In one of my methods I wanted to put randomly colored text on the screen. I put this class method in my Utilities.m class.


#define ARC4RANDOM_MAX      0x100000000
+ (UIColor *) randomColor {
    CGFloat red =  (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat blue = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    CGFloat green = (CGFloat)arc4random()/ARC4RANDOM_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

To set the color, just call


label.textColor = [Utilities randomColor];

Lazy Instantiation in iOS

I had a bunch of code like this in the init for my first view controller. Paul Hegerty had mentioned lazy instantiation in his Stanford CS193 courses. And the boilerplate code in the AppDelegate for creating Managed Object Contexts and Persistent Store Coordinator uses it a log. So I understood the concept, but I hadn’t used it for my own globals. After listening to his most recent class, I decided to convert all of my init code to lazy instantiation.

One benefit of lazy instantiation is that you don’t allocate resources until you need to use the object. In my case, a better reason is that I’m not cluttering up my view controller with code that initializes global variables. In the Model, View, Controller design pattern, initialization code really doesn’t belong in the controller. But even more important, since I check for initialization in the class that creates the variable, I can’t forget to initialize the variable.

Old Code


    if (![Globals sharedInstance].showmePict ) {
        [Globals sharedInstance].showmePict = @"Either";
    }
    
    if (![Globals sharedInstance].targetSoundDelay ) {
        [[Globals sharedInstance] resetTargetDelay:TARGET_SOUND_DELAY];
    }
    

    if ( !([Globals sharedInstance].targetSound) ){
#ifdef SHOWME_TARGET_SOUND
        [[Globals sharedInstance] resetTargetSound:SHOWME_TARGET_SOUND];
#endif
    
#ifndef SHOWME_TARGET_SOUND
        [[Globals sharedInstance] resetTargetSound:@""];
#endif
    }

New Code

In the singleton for globals.


- (NSString *)showmePict {
    
    if (!_showmePict ) _showmePict = @"Either";
    return _showmePict;
}

- (NSUInteger)targetSoundDelay {
    
    if ( !_targetSoundDelay ) [self resetTargetDelay:TARGET_SOUND_DELAY];
    return _targetSoundDelay;
}

- (NSString *)targetSound {
    
    if ( !_targetSound ){
    #ifdef SHOWME_TARGET_SOUND
        [self resetTargetSound:SHOWME_TARGET_SOUND];
    #endif
        
    #ifndef SHOWME_TARGET_SOUND
        [self resetTargetSound:@""];
    #endif
    }
    return _targetSound;
}

hdiutil

I recently started selling my software on Gumroad. For the Windows side, we just zipped up the .exe installer and uploaded it. For the Mac side, we first made a disk image where we laid out the files and then compressed them. You can do this with Disk Utility, but it’s a lot easier to do it on the command line.

The first thing you should do is put all the files you want to distribute into a folder. Then get info on the folder to see how much room you will need for your disk image. Round up a little. In the example below, I’m creating an image with 20MB. “ ProductName” is the name of the software, e.g. Match Ups!, or Train Time.


hdiutil create -megabytes 20 -fs HFS+ -volname ProductName ProductName.dmg

Once you have everything laid out how you want it, compress the image.


hdiutil convert -format UDZO ProductName.dmg -o ImageForDistribution.dmg

Where UDZO – UDIF zlib-compressed image. You can rename ImageForDistribution.dmg to anything you want.

Upgrading from Windows to a Mac

The last time I used Windows for more than a half-hour at a time was Windows 3.1, so temper what I say about Windows with your own experience. There are lots of superficial differences between Mac and Windows and one major difference. And that’s the way they handle the menubar. The first screen you see on your Mac has a menubar at the top of the monitor. There is an Apple and a bunch of menu commands. The stuff under the Apple never changes when you open new programs, but the stuff to the right of it does. The Apple is similar to the Start button on Windows. You can access the System Preferences and Sleep/Shut Down/Log Out there. There is also an item called Recent Items that is very handy for opening files that you recently worked on or Applications that you recently had open.

When you open a new program, the menubar at the top of the screen changes. It has the commands associated with the current program. All of the menubars start with File and Edit and all end with Help. In between, they differ but most have a View option. Programs that do text editing usually have a Format option. Here’s the big difference between Mac and Windows. When you open multiple documents on the Mac you don’t get new menubars, you get new document windows. Most programs lots of other little windows that they use for palettes. Sometimes they open automatically. Other times they don’t open until you select them from the menubar. You can move these around on the screen to wherever they make the most sense for you. Most of the time, when you quit an application, the next time it opens it will remember where you put the palettes and which ones were open.

One thing that I have heard is much different on Macs than on Windows is the way that folders are copied. On the Mac, if you drag a folder from one location to another and there is already a folder with the same name in that location, all of the contents of the existing folder will be replaced with the contents of the new folder. Same thing if you drag a file into a folder that has a folder with the name of the file. You will be prompted to replace it, but if you say yes, the folder no longer exists and there is a file there instead. I have been told that on Windows individual files are replaced if they have the same name. Existing files are not erased.

Don’t waste your money on anti-virus. Do keep vigilant about phishing attacks. They seem to have increased lately. My Windows guy recommends using the Microsoft anti-virus if you run Parallels for Windows.

I don’t do much fancy word processing, and when I do I use InDesign. I mostly use Bean http://www.bean-osx.com/Bean.html for formatted text. New Macs come with a free copy of Numbers and Pages which will open most Office documents. Pages and Numbers are very unintuitive to me, but if you are used to Microsoft products, I’ve heard that they are easy to pick up. Every once in a while someone sends me Excel spreadsheets and I use LibreOffice to open them. No one complains when I send them back, so I’m guessing it’s compatible. It’s also free.

If you do any audio processing download a free copy of Audacity.

Don’t bother downloading Flash or Adobe Reader. Most websites that use Flash have an HTML5 version that runs fine in Safari. Download a copy of Google’s Chrome browser for the sites that use Flash. Or you can turn on Developer mode and tell the website that you are an iPad. A lot of Mac users like Chrome better than Safari. Especially the geekier ones who like to use a lot of plug-ins.(Some of my demos only run in Flash and if you are a Stewart/Colbert fan you’ll need Flash to watch their clips.) There are also network TV shows that play better in Chrome and some YouTube clips won’t play without Flash.

You’ll also want to download Jumpcut for getting access to your clipboard. It lets you paste things you copied a while ago into the current document. I use it hundreds of times a day. Also free.

You also need Easy Envelopes for printing envelopes. It’s from Ambrosia Software or the Mac App store. Best $9.99 I ever spent. It’s a widget that installs on your dashboard (F12)

You should learn to use the dashboard. I have the current METAR (a Web Clip), a calculator, weather, a unit converter, and Easy Envelopes on my dashboard.

If people send you random videos (and you watch them) you’ll want to download VLC. It opens just about any video format.

If you want a flight simulator, purchase X-Plane.

There are some people who like to have everything on the desktop. I’m not one of them. I usually have whatever I’m currently working on there and then file it away when I’m done. The default place to store files is Documents. I have most of my stuff there. I created a folder called Current that I put in Documents. The name has a space in front of it so that it sorts first. I put all the stuff that I need to deal with in the near future in that folder. That way it’s not cluttering up my desktop and I don’t forget to deal with it.

Learn to use Time Machine. I have two Western Digital 3TB hard drives. I do a Time Machine backup on one every couple of days of them. The other one is in the hangar and I bring it home once a month to do a backup. That’s probably overkill for most people. If you organize your hard drive, you can probably make due with a Time Machine backup and a removable USB stick.

I’d recommend that you put anything that would be a real pain to replicate on a USB stick and keep it somewhere that your computer isn’t. I have my accounting data and my customer database backed up on USB sticks. Every time I go to the office I leave one stick and take home the other. Worst case I lose a couple of days of data. I have never personally had a catastrophic data loss, but I know plenty of people who have. YMMV

There are lots of right-click items that are really useful. If you are in a web browser, mail, or most programs that deal with words, right click on a word and you can do a dictionary lookup or search with Google. If you don’t have a mouse with a right mouse button, Control-click on the word. If you’ve misspelled the word, you get suggestions on how to spell it. Opening the spelling and grammar panel is also one of the options.

Vuescan for Mac will handle just about any scanner. It also has lots of scanning options. With a little work, Image Capture will do the same thing. You just need to download and install the TWAIN SANE Plugins. I haven’t needed to scan anything for a while, but it worked fine using Mountain Lion on two of my scanners that are at least 15 years old. They have plug-ins for Mavericks, but I haven’t tried them. http://www.ellert.se/twain-sane/

I also use Image Capture to get photos off my digital camera. For me, it works better than iPhoto. If you do lots of Photo work, you’ll want to get Aperture or Adobe’s Lightroom.