Monday, September 10, 2012

UIIMageView Add Image in iphone example

UIIMageView Add Image

"initWithImage" is a image declaring method that is used to declare the image while while creating a UIImageView.
Syntax of initWithImage method
- (id)initWithImage:(UIImage *)image;
a simple example
//declare the Image
UIImage *one = [UIImage imageNamed:@"myImage.png"];
//creating a UIImageView
UIImageView *imageView1 = [[[UIImageView alloc] initWithImage:one] autorelease];

//to add image on view
[self.view addSubview:imageView1];


You can write and run the above given lines of code into the "ViewDidLoad" method.

iPhone Single Tap and Double Tap Example

iPhone Single Tap and Double Tap Example

The example discuss about single tapping / double tapping on UIView and it also explains how to represent the UIIMage in different modes such as displaying image on the Top, center or bottom of the UIView. You can also zoom in or out the image on single or double tap… the "UIViewContentMode" implements all these methods with the certain modes as given below…
List of given UIViewContentMode
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
In the example, to detect the end of tapping we'll use the UITouch method "touchesEnded:withEvent:" that tell's the receiver that one or more fingers have been lifted from the associated UIView. We'll also write the method to count the Tap.
See the code given below…
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

//Get all the touches.
NSSet *allTouches = [event allTouches];

//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

switch([touch tapCount])
{
case 1://Single tap
imgView.contentMode = UIViewContentModeCenter;
break;
case 2://Double tap.

imgView.contentMode = UIViewContentModeLeft;

break;
}
}
break;
}

}

Download code

Dev iphone UIView Access Subviews in iphone example

UIView Access Subviews

In the example, you'll learn how to create and access a subview on UIView.
Syntax of adding subview at UIView.
- (void)addSubview:(UIView *)view;
Creating and accessing a subview : In the example we are creating & adding a UIImageView on the UIView as a subview programatically. The method can be written into the "viewDidLoad" method that do the additional setup after the loading view.
a simple example:
- (void)viewDidLoad {
[super viewDidLoad];

UIImage *one = [UIImage imageNamed:@"imagename.png"];
UIImage *two = [UIImage imageNamed:@"Krishna_Arjuna.png"];

//Creating a subview
UIImageView *imageView1 = [[[UIImageView alloc] initWithImage:one] autorelease];
UIImageView *imageView2 = [[[UIImageView alloc] initWithImage:two] autorelease];

imageView2.alpha = 0;

//Adding subview
[self.view addSubview:imageView1];
[self.view addSubview:imageView2];

}

Download Code

iPhone Pick images from Photo Library iphone example

iPhone Pick images from Photo Library

In this example application we are going to show you how to pick an image from photo library.

To choose the photo from the photo library we required to call three different delegates in the @interface class.
1.UINavigationControllerDelegate - We required this method to perform some actions such as pushed and popped that would not be on the scope of your view controller.
2.UIActionSheetDelegate - implements the button actions and any other custom behavior.
3.UIImagePickerControllerDelegate - This methods of this protocol notify your delegate when the user either picks an image or movie, or cancels the picker operation.
a Simple Example:
Header File - PhotoLibraryViewController.h
#import <UIKit/UIKit.h>
@interface PhotoLibraryViewController : UIViewController <UINavigationControllerDelegate ,UIActionSheetDelegate, UIImagePickerControllerDelegate>{

IBOutlet UIButton* openLibrary;
}

@property(nonatomic, retain)IBOutlet UIButton* openLibrary;
-(IBAction)openPhotoLibrary:(id)sender;
@end
Implementation file - PhotoLibraryViewController.m
#import "PhotoLibraryViewController.h"
#import <AudioToolbox/AudioServices.h>

@implementation PhotoLibraryViewController
@synthesize openLibrary;

- (void)dealloc {
[super dealloc];
[openLibrary release];
}

- (void)viewDidLoad {
[super viewDidLoad];
}

-(IBAction)openPhotoLibrary:(id)sender
{

UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Photo Library"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

if (buttonIndex == 0)
{
NSLog(@"ok One");

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];

}

if (!(buttonIndex == [actionSheet cancelButtonIndex])) {
NSString *msg = nil;

[msg release];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

@end
In the example, we are opening a photo library on button click. So you need to take a button at nib file and connect it with the appropriate action in file owner.
Run the application.. the output should be similar to given image.




Download code

Dev iphone UIButton setTitle:forState: in iphone example

UIButton setTitle:forState:

setTitle:forState: is the method of UIButton Class, which is used to set the title of the UIButton for different states. These states could be any from the given list…
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000

By default the state is normal.
Syntax of setTitle:forState:
- (void)setTitle:(NSString *)title forState:(UIControlState)state

The "setTitle:forState:" methods takes two parameter "Title" and "State".
Title: is a text that will be displayed on the UIButton for the given state, where as the "State" is the current form of the control. For example a button could be in any form selected or normal. The UIButton will always display the title for assigned state.
See the example below…
- (void)myButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];

[button setTitle:@"My Button" forState:UIControlStateNormal];

button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}

-(void)ButtonPressed:(id)sender
{
// here you can access the object which triggered the method
// for example you can check the tag value

NSLog(@"hello!!");
}

In the above example, the UIButton title is set for the state normal …so it'll show the title once the button get loaded.
Download Code

UITableViewCell Wrap Text - an Example

UITableViewCell Wrap Text - an Example

In the example, we are discussing about the setting of UITableViewCell to warp the text in multiple lines. Though you can do the setting directly through nib, but in the example we are doing it programatically.
UILineBreakMode is the property of UITableViewCell that is used to break the text or wrap the text in multiple lines. We are using the UILineBreakModeWordWrap property in the example.
List of other available UILineBreakMode properties:
- UILineBreakModeWordWrap = 0, // Wrap at word boundaries
- UILineBreakModeCharacterWrap, // Wrap at character boundaries
- UILineBreakModeClip, // Simply clip when it hits the end of the rect
- UILineBreakModeHeadTruncation, // Truncate at head of line: "...wxyz". Will truncate multiline text on first line
- UILineBreakModeTailTruncation, // Truncate at tail of line: "abcd...". Will truncate multiline text on last line
- UILineBreakModeMiddleTruncation, // Truncate middle of line: "ab...yz". Will truncate multiline text in the middle

a Simple Example:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

// set font style, modes & lines.
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3; // 0 means no max.


}

// Configure the cell.
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}
the Output is:

Download Code

Dev iphone Declaring Method in Objective C in iphone example

Declaring Method in Objective C

In Objective C we can declare two types of methods "Class Method" and "Instance Method" Where + sing denotes the Class method and - sign denotes the Instance method.
For example:
+ (void) printData;
- (void) printData;
The forgoing Objective C methods are declared without parameters. It only takes method type (+ & -), return type (void) and the method name (printData).
You can also declared a method with parameters. Objective C Method also accept more then one parameter. for example…
Objective C method declaration with single parameter:
+ (void) getId: (NSString *) yourId;
- (void) getId: (NSString *) yourId;

Objective C method declaration accepts more than one parameter:
+ (void) getId: (NSString *) yourId andName : (NSString *) yourName;
- (void) getId: (NSString *) yourId andName : (NSString *) yourName;