Saturday 20 October 2012

How to make a check button in the application without using image

In iOS application Xcode does not have an object of check button, which is available only in Mac applications. We can create a check button in the application by using unicode string.
The unicode string for check mark button is : 2611
and unicode string for uncheck button is : 2610

Using this string we can have a check mark or uncheck mark in the application, which is used for different states in UIButton.
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateNormal];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateHighlighted];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateDisabled];
[_checkButton setTitle:[NSString stringWithString:@"\u2610"] forState:UIControlStateNormal];
[_checkButton setTitle:[NSString stringWithString:@"\u2611"] forState:UIControlStateSelected];

This code can be wrtiten in viewDidLoad, where the initialization is done. And the state can be changed in the action method of this button, as shown in below method.
- (IBAction) click:(UIButton*)sender
{
sender.selected = !sender.selected;
}

As we have to change the state to selected for check, this line will do the work for that.

To identify the check or uncheck of the button, we need to just check the selected state of the button wether its YES or NO.

Happy Coding.

How to make use of Digital Color Meter application in MAC


We have an application in our MAC called "DigitalColor Meter". We can use this application into our application to get the color code for RGB.
Steps:
1)   Open Digital Color Meter application in MAC, can use spot light for easy search.
2)   Keep the required mode to "RGB as Percentage"
3)   Using the mouse pointer go to the required color on the screen.
4)   Note down the RGB values in percentage.
5)   In our code you can use the method 
[UIColor colorWithRed:0.663 green:0.855 blue:0.341 alpha:1]

Convert percentage values into fractions. like 53.5% will become 0.535. Use the respective RGB values in the code, and alpha value must be 1.
Run the application, you will get the exact color required.
For HTML users
In the "DigitalColor Meter" application set the mode to "RGB As actual value, 8-bit", so that you can get the RGB value as decimal number from 0-255. Note this RGB numbers.
  
Use RGB-HEX to get the hexadecimal number of the required color by inserting these RGB values.
for example:
R - 85   G - 20   B - 30
required color in hexadecimal is : #55141E
You can use this hexadecimal value, to get the required color in html code.

Happy Coding.

Friday 19 October 2012

delegates in Objective C

What is a Delegate?
Technically speaking, a delegate is just an object that has been assigned by another object as the object responsible for handling events.

An example with real time situation:
A xyz office has some 5 counters, you will enter into office go to counter 1 and submit the some details etc. And the counter 1 will tell you to wait for some time and move to counter 3 to finish to get your work complete. In this case counter 3 is your delegate which handles your events, which is referred by counter 1. Here the same counter 1 may also be the delegate, to handle your events.

Why Delegate is required?
In one word we can define delegate as “Call-back”. Means after performing some task, who, why and how will handle the events.

What is the use of this Delegate?
While building any applications or projects delegates play a very important roles. In some situations some tasks should be performed out of the area and after finishing we should get to know the results, whether task is achieved or in progress or stopped. These statuses can be accessed by using the delegates.

Predefined Example:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self cancelButtonTitle:@“OK” otherButtonTitles:nil];

The initializer of the UIAlertView class includes a parameter called the delegate. Setting this parameter to self means that the current object is responsible for handling all the events fired by this instance of the UIAlertView class. If you don’t need to handle events fired by this instance, you can simply set it to nil:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello”message:@“This is an alert view” delegate:nil cancelButtonTitle:@“OK” otherButtonTitles:nil];

If you have multiple buttons on the alert view and want to know which button was tapped, you need to handle the methods defined in the UIAlertViewDelegate protocol. You can either implement it in the same class in which the UIAlertView class was instantiated or create a new class to implement the method, like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);
}

To ensure that the alert view knows where to look for the method, create an instance of SomeClass and then set it as the delegate:

SomeClass *myDelegate = [[SomeClass alloc] init];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:myDelegate cancelButtonTitle:@“OK” otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];


How to create our own delegates?
To create your own delegate, first you need to create a protocol and declare the necessary methods, without implementing. And then implement this protocol into your header class where you want to implement the delegate or delegate methods.

A protocol must be declared as below:

@protocol ServiceResponceDelegate <NSObject>

- (void) serviceDidFailWithRequestType:(NSString*)error;
- (void) serviceDidFinishedSucessfully:(NSString*)success;

@end

This is the service class where some task should be done. It shows how to define delegate and how to set the delegate. In the implementation class after the task is completed the delegate's the methods are called.

@interface ServiceClass : NSObject
{
id <ServiceResponceDelegate> _delegate;
}

- (void) setDelegate:(id)delegate;
- (void) someTask;

@end


@implementation ServiceClass

- (void) setDelegate:(id)delegate
{
_delegate = delegate;
}

- (void) someTask
{
...
...
...
perform task
...
...
...
if (!success)
{
[_delegate serviceDidFailWithRequestType:@”task failed”];
}
else
{
[_delegate serviceDidFinishedSucessfully:@”task success”];
}
}
@end

This is the main view class from where the service class is called by setting the delegate to itself. And also the protocol is implemented in the header class.

@interface viewController: UIViewController <ServiceResponceDelegate>
{
ServiceClass* _service;
}

- (void) go;

@end



@implementation viewController

//
//some methods
//

- (void) go
{
_service = [[ServiceClass alloc] init];
[_service setDelegate:self];
[_service someTask];
}


That's it, and by implementing delegate methods in this class, control will come back once the operation/task is done.
If you liked this tutorial or found something wrong with it please let me know!
I would love to hear your experience with it.