Wednesday, July 28, 2010

iPhone: Add bar button item and set action method programmatically

UIBarButtonItem *rButton =[[UIBarButtonItem alloc] init];

rButton.title= @"someText";
[rButton setTarget:self];
[rButton setAction:@selector(aMethod)]; //aMethod defined in the class
self.navigationItem.rightBarButtonItem=rButton;

Tested & working in my project!

Happy Coding :)

Tuesday, July 27, 2010

iPhone : Time interval with a given date

NSDate *now = [NSDate date];
//refDate is the date you select from picker
NSDate *refDate = [datePicker date];

NSTimeInterval difference = [now timeIntervalSinceDate:refDate];
NSString *strTimeDiff =[[NSString alloc] initWithFormat:@"%0.0f",difference];

Monday, July 26, 2010

iPhone : Protocols

Pointy protocol, curved category
In objective C, Pointed brackets are associated with protocols. Curved brackets are associated with categories.

Protocols are Objective C's version of what is known in .NET as "interfaces".

Defining the protocol

@protocol MyProtocolName
//Method declarations go here
@end

there are no curly brackets. That's because variables go in curly brackets, and protocols have no variables associated with them.

Notice 'NSObject'. This actually means that the current protocol is a derivative of the NSObject protocol. (There is both an NSObject class and an NSObject protocol...this is the protocol...)

The concept of protocols: Protocols are a strange & interesting cross between interfaces and event definitions. In the languages (C#, VB.NET, Java etc… ) when you define an interface, you’re defining a common set of methods that objects must implement if they want to be considered to “implement” a given interface. One of the requirements of implementing an interface is that you must implement every method in the interface, either directly or via a subclass by marking the method as abstract.
Interfaces are use frequently for implementing “event callbacks”.

We basically define a standard set of methods/events, called a protocol, that our class will call. Then, when instances of our class are created, a class implementing our protocol, called a ‘delegate’, can register itself as a handler. Take the following protocol definition below:

@protocol MyProtocolDefinition
@required
- (void) Save:(NSString*) stringToSave;
@optional
- (void) OptionalMethod:(NSString*) parameter;
@end

The above code indicates that the Save() method is a required method to implement in classes implementing this protocol. However, the OptionalMethod is optional, meaning classes wanting to use this protocol only need to implement the Save() method, but they could also implement the OptionalMethod.

Using the protocol

In Objective C, you use the pointy brackets in the interface declaration (and by "interface" here, I mean the part of the class in the header file, following the class you extend.
For example, suppose your class was normally declared like:
@interface myClass : UIView

To specify that it implements a protocol, simply change it to this:

@interface myClass : UIView


Protocols [As variables]

In Objective C, you declare a variable this way:

id myNewVariable;

So the new type is "id". "id" is the generic object.

You can also use this notation when defining a method...e.g.

- (void) doSomethingWithThisObject: (id) aObject

Enjoy Coding passionately :)

Thursday, July 1, 2010

iPhone : Change font and label size in UIPIckerView

while using picker view in iPhone ,I face the challenge how to re size the text appearing in Picker view components.

Just do it!
Instead of using-

- (NSString *) pickerView:(UIPickerView *) pickerView

titleForRow:(NSInteger) row

forComponent:(NSInteger) component

{

}



//USE



- (UIView *)pickerView:(UIPickerView *)pickerView

viewForRow:(NSInteger)row

forComponent:(NSInteger)component

reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;

if (pickerLabel == nil) {
//label size
CGRect frame = CGRectMake(0.0, 0.0, 70, 30);

pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];

[pickerLabel setTextAlignment:UITextAlignmentLeft];

[pickerLabel setBackgroundColor:[UIColor clearColor]];
//here you can play with fonts
[pickerLabel setFont:[UIFont fontWithName:@"Times New Roman" size:14.0]];

}
//picker view array is the datasource
[pickerLabel setText:[pickerViewArray objectAtIndex:row]];

return pickerLabel;

}

Garg Praveen

iPhone:Add done button to number pad in iphone

Here! We will be knowing how to add 'Done' button on number pad in iphone. this is bit tricky..
Problem specification:
In IB where we can select keyboard type I have changed it to number pad and this code doesn't seem to work for me.

- (BOOL)textFieldShouldReturn:(UITextField *)textBoxName {
[textBoxName resignFirstResponder];
return YES;
}
Type: Number Pad
Return Key: Done

And Auto-enable done key is checked.


But there is no done button appears on number pad!!!!
Hopes are alive ..................
I got the solution :)

when you will see the number pad on iphone ,there is a blank space on left bottom.This is the place which I will use to put Done Image,and use it to get this number pad disappear ...
First add these two images to your resorce folder.









-(void)keyboardWillShow:(NSNotification *)note {

// create custom button

UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
// exactly of image size
doneButton.frame = CGRectMake(0, 163, 106, 53);

doneButton.adjustsImageWhenHighlighted = NO;

[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];

[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];

[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

// locate keyboard view

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

UIView* keyboard;

for(int i=0; i<[tempWindow.subviews count]; i++) {

keyboard = [tempWindow.subviews objectAtIndex:i];

// keyboard view found; add the custom button to it

if([[keyboard description] hasPrefix:@"
[keyboard addSubview:doneButton];

}

}



//// declare it in .h

-(IBAction) doneButton:(id) sender

{
//objNumberPad is the Keyboard
[objNumericPad resignFirstResponder];

}


//

- (void)viewDidLoad {

//Notification to add done buton on num pad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

}

- (void)viewDidUnload {

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

I'm using it in live application,Hope this will resolve your problem.