Archive for the ‘Objective C’ Category

31
Jan
+(BOOL)isOS4{
	NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"4.0" options: NSNumericSearch];
	if (order == NSOrderedSame || order == NSOrderedDescending) {
		return YES;
	} else {
		return NO;
	}
}

31
Jan
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

27
Jan
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0]; // This will get the path of the application directory.

NSArray *listArray = [[NSFileManager defaultManager] subpathsAtPath:documentsDirectory]; // This will store the list of files in the documents directory to the listArray.

, , ,

27
Jan

Consider a UITextView object, myTextView. Now, you can make the contents of the textview scrollable, but not editable using the following lines of code.

myTextView.editable = FALSE; // This will make the textview non editable.

myTextView.scrollEnabled = TRUE; // This will make the textview scrollable.

, ,

19
Jan

Consider a UITableVIew, myTableView and a UITableViewCell object, named myCell, representing a cell of myTableView.

Now you can get the indexpath of  myCell as follows:

NSIndexPath *indexPath = [myTableView indexPathForCell:myCell];

,

19
Jan

Consider a NSMutableString object mutableStr.

NSMutableString *mutableStr = [ [NSMutableString alloc]  initWithString:@"My name is Happy and I'm very happy."];  //  Creating and initializing NSMutableString object.

Now if you want to replace the “happy” in the string to “sad”, then

[mutableStr replaceOccurrencesOfString:@"happy" withString:@"sad" options: NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableStr length])]; // This will replace all both "Happy" and "happy" in the string to "sad".

Now the mutableStr value will be as follows:

My name is sad and I’m very sad.

 

If you want to replace only the “happy”, then you should do as follows:

[mutableStr replaceOccurrencesOfString:@"happy" withString:@"sad" options:NSLiteralSearch range:NSMakeRange(0, [mutableStr length])]; // This will replace only "happy" to sad.

Now the mutableStr value will be as follows:

My name is Happy and I’m very sad.

, ,

13
Jan

The symbol used in SQLite for not equal is ‘<>’

Consider a sample select query as follows:

SELECT fieldName FROM tableName WHERE referenceField <> 1 // THis will select the fieldName values from the table, tableName with referenceField value not equal to 1

, ,

06
Jan

Consider a UITextField object named testTextField.

Now we can align the content of the UITextField vertically as follows :

testTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // This will align the content to vertically center.

The following are the various vertical alignment modes that are available

UIControlContentVerticalAlignmentTop // For aligning the content to vertically top.

UIControlContentVerticalAlignmentCenter // For aligning the content to vertically center.

UIControlContentVerticalAlignmentFill // For aligning the content to vertically fill.

 

UIControlContentVerticalAlignmentBottom // For aligning the content to vertically bottom.

, ,

06
Jan

Consider a UITextField object named testTxtField.

By default the capslock button of the keyboard on respond to UITextField will be ON.

Now you can make the capslock button of the keyboard to OFF position using the following code.

 

testTxtField.autocapitalizationType = NO; // Here we are setting the autocapitalizationType to NO. By default the autocapitalizationType will be YES.

, , , ,

30
Dec

Consider a selector, ‘textFieldshouldChange:’ which responds to the text change in the UITextField.

Now you can restrict the text in the textField to numbers using the selector method as the following one.

-(void)textFieldshouldChange:(id)sender{

UITextField *txtField = (UITextField *)sender;

if ([txtField.text intValue]) {

NSLog(@"Integer");

NSString *str = [NSString stringWithFormat:@"%d",[txtField.text intValue]];

if ([str length] != [txtField.text length]) {

txtField.text = str;

}

}

else {

NSLog(@"Not Integer");

if ([txtField.text length] == 1) {

txtField.text = @"";

}

}

}

, ,