Saturday, July 23, 2011

Add UITextField Programmatically


//Add UITextField Programmatically 
CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *txtField = [[UITextField alloc] initWithFrame:textFieldFrame];
// default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.

[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setTextColor:[UIColor blackColor]];
[txtField setFont:[UIFont systemFontOfSize:20]];
[txtField setDelegate:self];
[txtField setPlaceholder:@"Add Your Placeholder Text Here"];
[txtField setBackgroundColor:[UIColor whiteColor]];
txtField.keyboardType = UIKeyboardTypeDefault;

Create a UISwitch Programmatically


//Add Below code in ViewDidLoad or LoadView


CGRect frame = CGRectMake(0.0, 0.0, 60.0, 26.0);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
[switchControl addTarget:self action:@selector(actionSwitch:) forControlEvents:UIControlEventTouchUpInside];

[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];

Friday, July 22, 2011

Create the UIScrollView Programmatically

With the help of  the UIScrollView we will able to  display content that is larger than the size of the application’s window. It enables users to scroll the view and to see the full contents.

//Creating the scroll view
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

//Create the views which are going to display in the scrollview.
int count= 4;
for (int i = 0; i
{
    CGFloat y = i * self.view.frame.size.width;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 0self.view.frame.size.widthself.view.frame.size.height)];
    view.backgroundColor = [UIColor greenColor];
    [scrollview addSubview:view];
    [view release];
}

// X will place every UIView exactly where the previous UIView 's frame ended.

// The x and y coordinates for creating the new 4 views will be.
-(0,0)(320,0)(640,0)(960,0)  

// We are increasing the x coordinate to 320, We can able to add the views horizontally one after other.
// ContentSize is nothing, it is size of the total view(Total size of 4 views) Content size will be (1280,480)
// Set the UIScrollView contentSize 
scrollview.contentSize = CGSizeMake(self.view.frame.size.width * count, self.view.frame.size.height); 

Move Image using UITouch methods in XCode iOS


Declare UIImageView in .h file

UIImageView *imageView;

And rest code will be in .m file

// On Load View Assign image to UIImageView
- (void)viewDidLoad
{
// Get image from resource folder with image name
UIImage *image = [UIImage imageNamed:@"Sample.png"];
// Set Frame size as per image height & width
CGRect frame = CGRectMake(00, image.size.width, image.size.height);
// Allocate memory and set frame for image view
imageView = [[UIImageView allocinitWithFrame: frame];
// Add Image view to main view
[self.view addSubview: imageView];
}

// On touch Image will move to touch location
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouchesanyObject];
// Get Touch location
CGPoint touchLocation = [touch locationInView:touch.view];
// Set touch location's center to ImageView
imageView.center = touchLocation;
}

// To Move image with touch You can 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
// get touch event
UITouch *touch = [[event allTouchesanyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageView) 
{
// move the image view
imageView.center = touchLocation;
}
}

Wednesday, July 20, 2011

Xcode iOS, Objective C: What is Protocol?

A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts.

Cocoa uses protocols to support interprocess communication through Objective-C messages. As Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.

An Example of a protocol is NSCoding, which has two required methods that a class must implement.
This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.

Another example is when using a tableview, your class implements thecellForRowAtIndexPath method which asks for cell content to insert into a table – thecellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.