Sujeev's Tech Blog

Tuesday, January 18, 2011

xCode iPhone self initiating variables.

lets say, I have a variable defined in .h, before using it on the .m file I have to initiate it, normally the initiating code is inserted some where on viewDidLoad if the variable is used through out the .m, however the dis-advantage with this format is that the variable will be initiated all the time the view is loaded, even when the variable is never used.

however with the following method, the variable is initiated on the first instance that its actually referred, there by if the variable is not used in a specific run the variable wont be initiated.

on the .h file

@interface ... {
CLLocationManager *locMan;
.
.
.
}

.
.
.
@property (nonatomic, retain) CLLocationManager *locMan;
.
.
.
@end

on the .m file

-(CLLocationManager *)locMan{
if (locMan != nil) {
return locMan;
}
locMan = [[CLLocationManager alloc] init];
locMan.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locMan.delegate = self;
return locMan;
}

Monday, January 03, 2011

UIAlertView and its delegate

Following is the normal use of UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:msgTitle message:soapResults delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

but lets say if we want to do some processing after a UIAlertView is dismissed, then to accomplish this,

first add a UIAlertViewDelegate to the viewController.
second use the following.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:msgTitle message:soapResults delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert setTag:127];
[alert show];

[alert release];

- (
void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == 127) {    
if (buttonIndex == 0) {
// do stuff
}
}
}


in the above code note the UIAlertView tag used as a identifier when there are more than one UIAlertViews