Thursday, September 27, 2012

IPhone: Pass data between view controllers

There are several ways to pass the data between the view controllers.
1.      Delegates & Protocols
2.      NSUserDefaults
3.      Database
4.      Singleton Object
Here I am going to explain using Delegates & Protocols.
First we will get an idea about the protocols and delegates.

Protocols

 Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:
·         To declare methods that others are expected to implement
·         To declare the interface to an object while concealing its class
·         To capture similarities among classes that are not hierarchically related
 Declaring a Protocol
You declare formal protocols with the @protocol directive:
@protocol ProtocolName
method declarations
@end
Optional Protocol Methods
Protocol methods can be marked as optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If you do not specify any keyword, the default is @required.
@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end

Delegates

iOS uses Objective-C delegates to implement the delegation pattern, in which one object passes work off to another. The object doing the work is the delegate of the first object. An object tells its delegate to do work by sending it messages after certain things happen.
Delegates are basically a way of extending the behavior of a class without needing to create subclasses. Applications in iOS often use delegates when one class calls back to another after an important action occurs.
For Java Developers:
Protocol is similar to Interface
Protocol
Interface
@protocol ProtocolName
-(void)methodName(NSString *):value;
@end
public interface ProtocolName {   
public void methodName(String value);
}
Declare protocol in .h file and import the .h file, wherever protocol has to be called
implements the interface and have a logic in a class

Sample Code : Here 
Screenshots:
View Controller Loaded
Entered Sample Text and Pressed Updated Button

Data transferred to Second View controller
 
Edited the Text and Pressed Back
Edited Text updated in first view controller
 

Java: Convert String to Hex ASCII


Convert String to Hex ASCII

To convert String to Hex ASCII, We can use format,
public static String format(String format, Object... args)

Returns a formatted string using the specified format string and arguments.
Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null

By using formatter class, we can convert char to hexadecimal integer.
Conversion
Argument
Category Description
'x', 'X'
integral
The result is formatted as a hexadecimal integer
'd'
integral
The result is formatted as a decimal integer  
'o'
integral
The result is formatted as an octal integer

Sample Code

public class ConvertStringToHexASCII
{
      public static void main(String[] args)
      {
            System.out.println(ConvertStringToHexASCII.convertStringToHexASCII("ABC"));
      }

      private static String convertStringToHexASCII(String value)
      {
            StringBuffer convertedValue = new StringBuffer();
            for (char c : value.toCharArray())
            {
                  convertedValue.append(String.format("0x%02x", (int)c));
                  convertedValue.append(", ");
            }
            return convertedValue.toString();
      }
}

Output: 0x41, 0x42, 0x43, 

Note: For ASCII Table Visit http://www.asciitable.com/