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
No comments :
Post a Comment