Home      Labs      Publications      People      Tools   

From CAGT

CS211 iPhone Programming Course Wiki - Problems and Solutions

Problems encountered in programming and solutions found by the professor, students, on the web, etc.

Contents

View Dimensions

portrait:

  • w/o status bar -- w: 320, h: 480
  • with status bar -- w: 320, h: 460
  • with status bar and navigation bar -- w: 320, h: 416

landscape:

  • w/o status bar -- w: 480, h: 320
  • with status bar -- w: 480, h: 300
  • with status bar and navigation bar -- w: 480, h: 268

bars: In PORTRAIT mode:

  • status bar: h: 20
  • navigation bar: h: 44
  • tab bar: h: 49
  • tool bar h: 44

In LANDSCAPE mode:

  • status bar: h: 20
  • navigation bar: h: 32
  • tab bar: h: 49
  • tool bar h: ??

Register as an iPhone Developer

http://developer.apple.com/programs/register/

Choose the free option when you register.

If you have your own Mac, download the iphone SDK (software development kit).

Load Class Apps on your iPhone/iPod Touch

Instructions for Loading Apps on your iPhone


Create a CS lab computer account

https://weblogin.bu.edu/accounts/create?_hostname=cs

Where to save your apps on the CS lab Macs so I can check them

/cs/coursedata/cs211/<username>/

Identify a UIButton without using the title

When you add a UIButton to a view in Interface Builder, open the Inspector in the Attributes tab. Go down to the section labeled View and the attribute labeled Tag. There will probably be a zero there. You can make this any integer and then save the .xib file. The following code will retrieve the tag number which can be used, for example, in a switch statement:

 -(IBAction)buttonPressed:(id)sender{
     NSInteger tag = [sender tag];
     switch(tag){
       case 0: code...
         break;
       case 1: code...
         break;
       default: code...
         break
     }
  }

To see all the properties available to a variable

Type the variable name and the dot, then press escape. A list of the properties pops up.

To see all the possible word completions

Type part of a word and press escape. A list of possible completions pops up.

System problems

Call Wes x39777

How does the index of a subview work?

The apparent behavior is the following.

 1. indices for subviews run from zero to at most the number of subviews
 2. a subview with index zero will be below every other subview (and so possibly hidden)
 3. a subview with the highest index is above every other subview (and so completely visible)
 4. a subview can’t be assigned below its parent view.
 5. if two subviews have the same index, the second one assigned the index appears below the first one assigned the index.  
    It is as if the index of the first subview has been increased by one.
 6. if the second subview is removed, it is as though the index of the first subview then decreases by one.
 7. a "forced" change in subview index in this way can cause a cascade of increases or decreases in the index of other subviews.

Using retain and release

Nice discussion at http://stackoverflow.com/questions/156243/object-allocate-and-init-in-objective-c

How to assign an image to a UIImageView programmatically

How to add primitive objects (int, Bool, floats) to an NSArray

Use NSNumber

How to put music files into an app

Can @synthesize be used without @property?

No.

Can both @property and @synthesize be omitted?

Yes. Don't use dot notation and explicitly code the getter and setter.

How to use fast enumeration

Does a subclass inherit conforming to a protocol from the superclass?

Yes. But, for NSCoding and NSCopying, the programmer must write additional code to handle new instance variables in the subclass.

How to debug EXC_BAD_ACCESS error

Set the NSZombieEnabled environment variable for the executable.

See the following:

http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

Using UIScrollView

Use the following steps:

  • add a UIScrollView instance variable, scrollView, to the controller class or to the view
  • if using Interface Builder for the superview,
    • set the size of the UIScrollView object to be the same as the available window size in the superview (but in no case, larger than the iphone window).
    • make scrollView an IBOutlet in the Controller class so its contentSize can be changed
    • connect the File's Owner outlet to the UIScrollView object in IB.
  • in viewDidLoad, set the contentSize. For example:

[self.scrollView setContentSize:CGSizeMake(720,220)]; //last two are width, height

  • add any subviews to the scrollView either in IB or programmatically

How to install the app icon

The app icon, which shows up on the main iPhone screen, is installed as follows:

1. create a png file with size 57 x 57 pixels
2. copy the file into the resources folder in Xcode
3. open info.plist and enter the file name for the Icon file key
4. to turn off automatic shading at the top of the icon, open info.plist in the Property List Editor.  
   Add a new key called UIPrerenderedIcon. 
   In the edit menu, choose Boolean. Put a check in the box in the value column.  Save the file and rebuild.  
   If the image doesn't change in the simulator, remove the program from the simulator 
   (place pointer over icon and hold down mouse button until the Xs appear and then remove).  
   Rebuild.

autoreleased with no pool in place error

This usually occurs if your app has created a new thread without also creating a new autorelease pool. Even if you don't call autorelease for an object, many class methods that return an object do call autorelease.

Example when using an NSTimer to call a method (which creates a new thread):

self.movingBusTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f                                                                     
                               target:self
                               selector:@selector(callUpdateMovingBusDisplay:)
                               userInfo:nil
                               repeats:YES];

In the @selector method, include an autorelease pool because this starts a new thread:

-(void) callUpdateMovingBusDisplay:(NSTimer *)theTimer{
         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
         [self updateMovingBusDisplay];
         [pool release];
}

Other examples include:

 performSelector:@selector(someMethod:) withObject: afterDelay:
 
 performSelectorInBackground:@selector(someMethod:) withObject:
   
 detachNewThreadSelector:@selector(someMethod:) toTarget: withObject:
Protein Engineering