Programming in Objective-C: Q&A

Q: How to vertically centre-align the text in textField?
A: [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

Q: How to make a dialogue window (i.e. add textFields) with AlertView?
A:
UIAlertView *describeWP = [[UIAlertView alloc] initWithTitle:@"Add a Waypoint" message:@"\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
UITextField *wpName = [[UITextField alloc] initWithFrame:CGRectMake(42, 45, 188, 25)];
[wpName setBackgroundColor:[UIColor whiteColor]];
[wpName setPlaceholder:@"Name your waypoint"];
[describeWP addSubview:wpName];
UITextField *wpDescrip = [[UITextField alloc] initWithFrame:CGRectMake(12, 80, 260, 55)];
[wpDescrip setBackgroundColor:[UIColor whiteColor]];
[wpDescrip setPlaceholder:@"Talk about it, e.g. tips, feeling, why excited ..."];
[describeWP addSubview:wpDescrip]
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0, 90);
[describeWP setTransform:moveUp];
[describeWP show];
[describeWP release];
[wpName release];
[wpDescrip release];

Q: Error: “missing required architecture i386 in file” for each framework.
A: Delete the following line from the project.pbxproj file:
FRAMEWORK_SEARCH_PATHS = ("$(inherited)", "\"$(DEVELOPER_DIR)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/System/Library/Frameworks\"", );

Q: How to detect the zooming level after the user finger-changed it and let the map keep the level?
A:

Q: When using mapView.showsUserLocation=YES; the error says:

[MKUserLocation annotationType]: unrecognized selector sent to instance

… What is wrong?
A: Put the line: if ([annotation isKindOfClass:MKUserLocation.class]) return nil; in the MKAnnotationView method, then it is fine.

Q: How to access the GPS coordinates in the iPhone from the programme?
A: Add CoreLocation framework, and #import in the .h. Set your view controller as CLLocationManagerDelegate, and CLLocationManager *locationManager; in the interface. Set basic parameters:
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter=kCLDistanceFilterNone; // Every move
locationManager.desiredAccuracy=kCLLocationAccuracyBest;// HundredMeters; Kilometer; ThreeKilometers
[locationManager startUpdatingLocation];

With a method:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ ...
yourGPS=newLocation.coordinate;
[self showOnMap:yourGPS];
}

Q: How to send a message (string) to twitter from iPhone.
Q: The main commands are:
NSURL *twitURL=[NSURL URLWithString:@"http://yourusername:yourpassword@twitter.com/statuses/update.xml"];
NSMutableURLRequest *twitRequest=[[NSMutableURLRequest alloc] initWithURL:twitURL];// cachepolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[twitRequest setHTTPMethod:@"POST"];
[twitRequest setHTTPBody:[[NSString stringWithFormat:@"what you want to tweet"] dataUsingEncoding:NSASCIIStringEncoding]];

Q: How to use background image for tableView?
A: There are 2 ways of doing it:
Method 1: [tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.JPG"]]];
Method 2: drag yourImage.jpg to the separate .xib file’s view window, to have a UIImageView with the image, then Layout->”Send to back”, then use the line: tableView.backgroundColor=[UIColor clearColor]; to make the tableView transparent.
Note that you need a separate .xib to enable the “Send to back”, i.e. it is greayed out if you do it in MainWindow.xib.
Another difference: the background image from Method 1 moves with the tableView, and the background image from Method 2 is dead still.
Refer to here for more details.

Q: How to use UIImagePickerController :
A:
UIImagePickerControllerSourceTypeCamera : to load the camera
UIImagePickerControllerSourceTypePhotoLibrary : to load images from library
UIImagePickerControllerSourceTypeSavedPhotosAlbum : loads all images saved

Q: How to convert the ‘string1’ defined by ‘NSString *string1’ to the ‘what1’ for ‘const char *stringBytes=what1’? in order to be written into file by:
dataToWrite = [NSMutableData dataWithBytes:stringBytes length:strlen(stringBytes)];
A: No need to convert to ‘const char’ to get the ‘what1’ and then ‘stringBytes’ in the middle. Just use the line below to directly convert the ‘string1’ to be NSData type:
dataToWrite=[string1 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
Need to change the definition of ‘dataToWrite’ from NSMutableData to NSData.

Q: How to write a line showing the current standard time in a file?
A: To get the current time to be a string:
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setDateStyle:NSDateFormatterMediumStyle];
[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSDate *timeNow = [NSDate date];
NSString *stringTimeNow = [timeFormatter stringFromDate:stringTime];
timeNow = stringTimeNow;

Q: How to detect the input/data from a wireless link, e.g. detect a friend’s coordinate and pop up a message if it is closeby?

Leave a comment