Archive for the ‘Objective C’ Category

09
Nov

It Shows a simple alert with OK button.

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

08
Nov

The following Objective C script clearly explain the usage of NSMutableString.

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *name = @"Chris Philip";
NSString *job = @"iPhone Developer";
NSMutableString *person = [NSMutableString stringWithFormat: name];
[person appendString: @", "];
[person appendString: job];
NSLog(@"%@",  person);
[pool drain];
return 0;
}

08
Nov

The following Objective C program will convert NSString to NSNumber and returns number in : integerValue, floatValue, doubleValue etc.

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *name = @"150";
NSNumber *num = [NSNumber numberWithInteger: [name integerValue]];
NSLog(@"%d", [num integerValue]);
[pool drain];
return 0;
}

05
Nov

The following script will show the usage of mutable array in Objective C.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:@"One" atIndex: 0];
[array insertObject:@"Two" atIndex: 1];
[array insertObject:@"Three" atIndex: 2];
NSLog(@"Total elements in array : %d", [array count]);
NSLog(@"array elements are : %@", array);
[pool drain];
return 0;
}

04
Nov

The following Objective C script will convert an integer to NSString object.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int int_val = 10;
NSString *str1 = [NSString stringWithFormat:@"%d", int_val];
NSLog(str1);
[pool drain];
return 0;
}

03
Nov

The following Objective C program prints date and time with NSDate.

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *today = [NSDate date];
NSLog(@"Today : %@", today);
[pool drain];
return 0;
}

01
Nov

The following program will print the index of an NSArray using indexOfObject:

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * str1 = @"One";
NSString * str2 = @"Two";
NSString * str3 = @"Three";

NSArray * array = [NSArray arrayWithObjects : str1, str2, str3, nil];
NSInteger index1 = [array indexOfObject : str1];
NSInteger index2 = [array indexOfObject : str2];
NSInteger index3 = [array indexOfObject : str3];

NSLog(@"%@ found at index %d", str1, index1);
NSLog(@"%@ found at index %d", str2, index2);
NSLog(@"%@ found at index %d", str3, index3);
[pool drain];
return 0;
}

01
Nov

The following Objective C Program will show you how to create an NSArray:

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
[array release];
[pool drain];
return 0;
}

29
Oct

Some times we don’t want a keyboard is showing while experimenting with the iPhone project.use the following code to hide the keyboard when a button is clicked.

In (filename)ViewController.h
add the following

@interface filename:UIViewController<UITextFieldDelegate>
{
}

and in .m file
add as

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[fieldname or button name resignFirstResponder];
return YES;
}

Here what we do is,when you click the text field or button,keyboard will show.so if you click the return,it will hide.

29
Oct

The following program will show you how to concatenate strings in Objective C:

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *str1 = @"Hello ";
NSString *str2 = @"World!";
NSString *str3 = [str1 stringByAppendingString: str2];
NSLog(str3);
[pool drain];
return 0;
}