use of Delegate in iphone sdk - iphone

Can somebody explain me how exactly does the delegate work in iphone sdk.....???
A simple example how to use the delegate and what are the advantages of using delegate.

Delegate pattern is used widely in iPhone SDK. Consider the examples:
You are running an animation. The underlying system handles the animation for you. But it is natural that you want to do something when the animation is over (say, you want to activate a button or show some text when animation is over). Now how the animation system will know what to do when animation is over? After all this is your custom task. So you will configure a delegate for the animation and the system will call that delegate method when the animation is over. Obviously you will do your custom tasks in this delegate method.
You have a text field and you want to know when the user have tapped or edited something in the field. How you will know that? You will configure a delegate for your text field and predefined delegate method will be called by the UITextField class when that particular field is edited or tapped.
Forget UIApllicationDelegate? The system does the job of loading and running the app. How it will tell you that it's initialization have finished and you can now run your code? It will call applicationDidFinishLaunching method of your app delegate.
You are making an asynchronous http request. After loading the data, your delegate method will be called so that you can now work with the data.
There are many more examples. In order to use delegate, you will require to specify the delegate object and sometimes the selector also. What exactly is needed to be done is dependent on what are you doing. That is, configuring an animation delegate is different from configuring a text field delegate. But the general procedure is same, that is you need to specify your delegate object.
Example code for animation :
CATransition *animation = [CATransition animation];
[animation setDelegate:delegate]; // here delegate is your delegate object
After animation is over, your delegate object's - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag will be called and you will do your customization in this method.

Delegates are a way to decouple message senders and receivers. Rather than a message publisher having to #import the definitions of all the classes that might have an interest in the message, The publisher instead defines a delegate type, and calls a method on that delegate in order to send messages. The receiver class then implements the delegate.

Wikipedia has both an explanation and examples :)
In software engineering, the delegation pattern is a design pattern
in object-oriented programming where
an object, instead of performing one
of its stated tasks, delegates that
task to an associated helper object.
It passes the buck, so to speak
(technically, an Inversion of
Responsibility). The helper object is
called the delegate. The delegation
pattern is one of the fundamental
abstraction patterns that underlie
other software patterns such as
composition (also referred to as
aggregation), mixins and aspects.

Basic definition :
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
more details
scenario (used in message passing) :
Suppose object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action. This is how delegation works.
With the help of protocols we can achieve delegation in iOS. Here is the
code :
ViewControllerA.h
#import <UIKit/UIKit.h>
#interface ViewControllerA : UIViewController
{
}
#end
ViewControllerA.m
#import "ViewControllerA.h"
#import "ViewControllerB.h"
#interface ViewControllerA ()<SimpleProtocol>
#end
#implementation ViewControllerA
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self performSelector:#selector(delegatingWorkToControllerB)withObject:nil afterDelay:3.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)delegatingWorkToControllerB{
ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
vcB.delegate = self;
[self presentViewController:vcB animated:YES completion:^{
}];
}
#pragma mark - SimpleProtocol Delegate Method
-(void)updateStatus:(NSString*)status {
NSLog(#"%#",status);
}
#end
ViewControllerB.h
#import <UIKit/UIKit.h>
#protocol SimpleProtocol<NSObject>
-(void)updateStatus:(NSString*)status;
#end
#interface ViewControllerB : UIViewController
{
}
#property(nonatomic, unsafe_unretained)id<SimpleProtocol>delegate;
#end
ViewControllerB.m
#import "ViewControllerB.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self performSelector:#selector(informingControllerAAfterCompletingWork) withObject:nil afterDelay:3.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
-(void)informingControllerAAfterCompletingWork{
//you can perform some task here and after completion of the task you can call this to notify the previous controller
[self.delegate updateStatus:#"controller B work has done.. update successfull :)"];
//dismissing the view controller
[self dismissViewControllerAnimated:YES completion:^{
}];
}
#end
Working Example : code

Related

how to change WebView with buttons in separate View Controller

I need to change the URL of a WebView (located in WebViewViewController) using buttons in View Controller 1 (called PracticeViewController). I totally understand how to change the web View when it is in the same View Controller as the buttons, but I don't understand how to get the buttons to affect the WebView when the WebView is in a different ViewController than the buttons. Below is the code I currently have:
PracticeViewController.h
#import <UIKit/UIKit.h>
#interface PracticeViewController : UIViewController
- (IBAction)passGoogleButton:(id)sender;
- (IBAction)passYahooButton:(id)sender;
- (IBAction)passBingButton:(id)sender;
/* ok I tried to make this as clear as possible, and you can look at the other files if you don't understand what I mean, but at its core I want to change the web site loaded by the Web View by clicking on the buttons in the other View Controller */
#end
space (BTW is there an easier way to post on StackOverFlow Besides pressing space 4 times for every line of code? am I missing something?)
PracticeViewController.m
#import "PracticeViewController.h"
#import "WebViewViewController.h"
#interface PracticeViewController ()
#end
#implementation PracticeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passGoogleButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.google.com";
/* This supposedly works on a tutorial I saw, but Xcode flags it with the error 'Property webSiteURL not found on object of type 'WebViewViewController'' */
}
- (IBAction)passYahooButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.yahoo.com";
//same error as above
}
- (IBAction)passBingButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.bing.com";
//same error as above
}
#end
space
WebViewViewController.h
#import <UIKit/UIKit.h>
#interface WebViewViewController : UIViewController
{
UIWebView *myWebView;
NSString *webSiteURL;
}
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#property (strong, nonatomic) NSString *webSiteURL;
/* I don't entirely understand what 'strong' means but I know it has to do with memory retention and that it (supposedly) should be used when I wan to transfer the value of the string 'webSiteURL' to the other ViewController */
#end
space
WebViewViewController.m
#import "WebViewViewController.h"
#interface WebViewViewController ()
#end
#implementation WebViewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webSiteURL]]];
/* As you can see above, the WebView loads from the string 'webSiteURL' but I can't figure out how to assign different values to it based on what button is clicked in the other View Controller so that it will run in ' - (void)viewDidLoad.' I already know how to transfer the value of the string to other things within another view controller (like into a label or a text field) but what I really need to know is how to get the string 'webSiteURL' to change before the view is loaded */
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have the right idea but you are missing a step here. You are never creating an instance of webViewController.
What you need somewhere is...
webViewcontroller *MyWebController = [[webViewController alloc] init];
It depends on how you are presenting the WebViewController. If you simply want the button to pop up the WebViewController modally you could use the following:
- (IBAction)passGoogleButton:(id)sender {
webViewController *myWebVC = [[webViewController alloc] init];
[myWebVC setWebSiteURL:#"http://www.google.com"];
[self presentViewController:myWebVC animated:YES completion:nil];
}
So, you can see here. You create the instance of the webViewController called myWebVC. Then you pass the string to it. So when myWebVC loads and it hits viewDidLoad, it will use the string that you passed in already and load the web view with that content. Hope that helps.
Also, Make sure you #synthesize your properties in your .m files.
You can use nsnotificationcenter to accomplish this task.
First register to notificationcenter in WebViewController, then send notification from another controller.
Here's a sample for registering to notificationcenter in WebViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(nMessageOpenURL:) name:#"nMessageOpenURL" object:nil];
This is the code to run the logic when you receive the notification. (in WebViewController)
- (void)nMessageOpenURL:(NSNotification *)note {
//run the logic in here.
}
And finally this is the code where you trigger the notification.
[[NSNotificationCenter defaultCenter] postNotificationName:#"nMessageOpenURL" object:nil];
Here's the Apple documentation.
I got to know that you are trying to pass the value of the string of url between two view controllers but you are not getting it right!!
So if you are using storyboard you can try out passing this value using Segue. For Eg: you can use prepareForSegue to set the different values using if-else construct.
And if you are not using segue the best practice to pass those values will be using delegate.
Just create a delegate method and pass the url string as an argument.
Hope this works :)

UIPageViewController check when swiped back or forward

I want to keep track of the index using the UIPageViewController. Whenever I swipe I need to index++ or index--. This delegate method gets called whenever you swipe back or further:
- (void)pageViewController:(UIPageViewController *)pvc didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
// If the page did not turn
if (!completed)
{
// You do nothing because whatever page you thought
// the book was on before the gesture started is still the correct page
return;
}
// I want to check here whenever the page was swiped back or further
}
How do I check in this method if the user swiped back or further? I know there are the 2 DataSource methods "viewControllerAfterViewController" and "viewControllerBeforeViewController" but I cannot check if the page transition has completed (and I can do this in the above method) any idea how I could know if the user swiped back or further in the above method?
use protocol:
MyClass : UIViewController <UIPageViewControllerDataSource,UIPageViewControllerDelegate
declare a atributes:
#property(nonatomic) NSInteger currentIndex;
#property(nonatomic) NSInteger nextIndex;
and in the methods:
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
NewsTableViewController *controller = [pendingViewControllers firstObject];
self.nextIndex = [self.arrViews indexOfObject:controller];
}
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
if (completed) {
self.currentIndex = self.nextIndex;
}
self.nextIndex = 0;
}
there you will have the current page.
Thanks to Corey Floyd in enter link description here
According to the documentation there does not appear to be a way to tell whether the user has swiped the page forward or backward. The boolean 'finished' will tell you whether or not the user has completed the page turn.
A workaround:
Create an int variable and using the viewControllerAfterViewController and viewControllerBeforeViewController methods either increase or decrease the value of the variable. Use that to test whether or not they moved forward or backward.
Edit: You could use presentationIndexForPageViewController from the documentation
Edit 2: Check this link here There is a method named setViewControllers:direction:animated:completion: the direction will be either UIPageViewControllerNavigationDirectionForward or UIPageViewControllerNavigationDirectionReverse
Edit 3: Code - This is assuming you know which view controller will be called by either going forward or backward:
Create a variable on your appDelegate and a setter method:
int indexVar;
- (void)setIndex:(int)indexVar;
Then on your view controllers (forward or backward) either increase the value or decrease the value (viewDidLoad):
(AppDelegate *)[[UIApplication sharedApplication] delegate] setIndex:<whatever>];
Something along those lines. This won't be an exact way to accomplish your goal, but hopefully it will get you headed in the right direction.
I did it by creating protocols in each of my ViewController classes, with the protocol method called in the viewWillAppear method. Then in the PageViewController whenever I instantiate one of the view controllers I set its delegate to be the PageViewController.
This is the 3rd ViewController in my project(Note that I've done this in each of my view controllers)
#class ViewController3;
#protocol ViewControllerPageNumber <NSObject>
-(void)viewWillAppearMyPageNumber:(int)myPageNumber;
#end
#interface ViewController3 : UIViewController
#property (nonatomic, weak) id <ViewControllerPageNumber> delegate;
#end
and in the .m file in the viewWillAppear method
-(void)viewWillAppear:(BOOL)animated{
[self.delegate viewWillAppearMyPageNumber:3];//The 3 will be different for each ViewController
}
Next, in the PageViewController.m, whenever I instantiate a view controller I set its delegate to be self( or PageViewController). viewCons is just an array of strings with my viewControllers names.
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
id vc = [[NSClassFromString([viewCons objectAtIndex:index]) alloc] init];
if([vc isMemberOfClass:[NSClassFromString(#"ViewController3") class]]){
ViewController3 *vc3=(ViewController3 *) vc;
vc3.delegate=self;
}else if([vc isMemberOfClass:[NSClassFromString(#"ViewController2") class]]){
ViewController2 *vc2=(ViewController2 *) vc;
vc2.delegate=self;
}else if([vc isMemberOfClass:[NSClassFromString(#"ViewController") class]]){
ViewController *vc1=(ViewController *) vc;
vc1.delegate=self;
}
return vc;
}
Finally, I'm implementing my custom delegate method, which in my case is refreshing labels' text I have set on top of the PageViewController.
-(void)viewWillAppearMyPageNumber:(int)myPageNumber{
[self refreshLabelsOnCurrentPageWithIndex:myPageNumber];
}
I think the easiest solution is to to create an own ViewController class with a property that keeps track of the currently shown index. In most of the cases I need a custom ViewController for my PageViewController anyways. In my example this is the following class.
#interface PageZoomViewController : UIViewController
#property (nonatomic) int pageIndex;
#end
Then in the viewControllerAfter/Before methods you can pass the index to the new page.
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
int nextPageIndex = ((PageZoomViewController *)viewController).pageIndex-1;
PageZoomViewController *controller = [[PageZoomViewController alloc] initWithPageViewControlParent:self andFrame:[self frameForPagingScrollView] andPageIndex:nextPageIndex];
return controller;
}
When the animation for the next page finished you can easily set the current index like this.
-(void)pageViewController:(UIPageViewController *)thePageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if(completed) {
index = ((PageZoomViewController *)thePageViewController.viewControllers[0]).pageIndex;
}
}
Hope this helps!

MainViewController init

I have set up a utility application and I found that the init method of MainViewController is not called automatically. Is there some other method that is set up automatically by XCode for initialization? Or if I have to add it, where would I add it? I was just going to call init manually from an IBOutlet method but I would prefer that the initializations be done when the view is first initialized.
Not sure where you'r going but (generated) .xib files are called using the withCoder init.
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
//self implementation
}
return self;
}
See the Utility Template in Xcode.
And check this method called..
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
check it..

Objective-C on iPhone - tab bar crash

I'm relatively new to Objective-C and coding. I've tried doing a little dev on my own but have now become stuck on what is probably a rookie error.
I've created a tab bar controller with 5 views, one such view is a UIWebView. I've got the Webview working and it loads, but when I select a different tab, the app crashes. Please find my code below and any help would be appreciated!
#import <UIKit/UIKit.h>
#interface LiveViewController : UIViewController {
IBOutlet UIWebView *liveView;
}
#property (nonatomic, retain) UIWebView *liveView;
#end
#import "LiveViewController.h"
#implementation LiveViewController
#synthesize liveView;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[self.liveView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.britishseapower.co.uk/live/"]]];
[super viewDidLoad];
}
- (void)webViewDidStartLoad:(UIWebView *)liveView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)liveView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)liveView:(UIWebView *)liveView didFailLoadWithError:(NSError *)error
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
#"<html><center><font size=+5 color='red'>An error occurred:<br>%#</font></center></html>",
error.localizedDescription];
[self.liveView loadHTMLString:errorString baseURL:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
if ( [self.liveView loading] ) {
[self.liveView stopLoading];
}
self.liveView.delegate = nil; // disconnect the delegate as the webview is hidden
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)dealloc {
[liveView release];
[UIWebView release];
[LiveViewController release];
[super dealloc];
}
#end
Many thanks,
Ryan
[UIWebView release]; [LiveViewController release];
This is what make your app crash.
It's not valid to send a release message to a class itself.
What you've done with [liveView release]; is enough (with the call to [super dealloc];.)
You should also set the delegate to nil in the dealloc method as in the viewWillDisappear method self.liveView.delegate = nil;. This way you're sure to avoid any further message sent to the LiveViewController from the UIWebView.
You should read a bit more of documentation on Objective-C to better understand how it works.
Not sure if this is related but I noticed that you aren't setting yourself as the delegate anywhere in code which means that it must be connected in Interface Builder. Now when the view disappears, you are breaking that connection, but if the view were to re-appear and wasn't previously unloaded that connection will remain broken.
One of the most common reasons why an app may crash is to refer to or send a message to an object that has been already released from the memory. And this type of bug can be easily located using NSZombieEnabled and looking into the console message. So if you haven't already tried that, that's the first thing you must do.
The problem could be in LiveViewController but could be in the other view controllers as well. I wouldn't believe the problem is 100% in LiveViewController because the view controller wouldn't try releasing its view when the view is not shown unless it gets a memory warning. And you run the app using the simulator, it's unlikely it will have a memory warning unless you simulate one.
You would probably know that a view controller never create a view unless the view is used by an object. One of the other view controllers may have a silly bug in its view loading process which causes a crash. Or, you might have released another view controller by mistake. Make 100% sure that the other view controllers have no problem showing their views on their own, when you keep changing between their views (without showing LiveViewController).
So what I would do is to try NSZombieEnabled and check if it accesses a released object, and if it does, what the object is. Also, I will make a double check that the problem is related to LiveViewController. If it doesn't help I would log a message when LiveViewController and its liveView is deallocated (for liveView you need to subclass it). Because delegate property almost always does not retain an object, if the LiveViewController object is released (which shouldn't happen) and liveView still has a reference to it in the delegate property it will make a crash.
Crashes like this are almost always related to releasing an object that has already been released and deallocated.
Let XCode help you find the error. In XCode 4:
- In the toolbar, select the scheme list, and select 'Edit Scheme'
- Select the 'Run Your.app' in the list on the left.
- Under 'Environment Variables', add the following name/value pairs in the appropriate columns:
CFZombieLevel 3
NSZombieEnabled YES
Now when debug your app, you will get a message telling when -release is called on an object that already has a -retainCount of zero. Now you have a good clue to start your investigation.
Note that these flags prevent objects from being deallocated, so it is best to turn them on as needed to prevent out of memory errors.

Strange custom delegate actions

Ok -- this one is weird. I have a singleton class that loads information from an XML file. I am using a delegate definition as follows (I define the delegate in a separate header file to make life easier):
#protocol ResourceClassDelegate <NSObject>
#optional
- (void)picturesDidStartLoading;
- (void)picturesDidFinishLoading;
#end
In the resource file, the delegate is defined correctly (I believe):
#property (assign) id<ResourceClassDelegate> delegate;
When using the delegate, the code in the resource class is as follows:
-(void)refreshPiecesOfHistoryWithOperation {
NSLog(#"Operation Started");
if ([delegate respondsToSelector:#selector(picturesDidStartLoading)])
[delegate picturesDidStartLoading];
self.picturePacks = [HistoryXMLParser loadPicturePacks];
[self.allPiecesOfHistory removeAllObjects];
// now lets put all of them in one big file...
for (PicturePack *pp in self.picturePacks) {
for (int ct = 0; ct < [[pp piecesOfHistory] count] ; ct++) {
[self.allPiecesOfHistory addObject:(PieceOfHistory *)[[pp piecesOfHistory] objectAtIndex:ct]];
}
}
NSLog(#"Operation Ended");
if ([delegate respondsToSelector:#selector(picturesDidFinishLoading)])
[delegate picturesDidFinishLoading];
}
Now... in the class that is listening to the delegate, it is assigned:
- (void)viewDidLoad {
[super viewDidLoad];
// now for the part that makes the loading all happen...
[[ResourceClass sharedResourceClass] setDelegate:self];
}
And in the listening class, the methods are defined....
#pragma mark ResourceClassDelegate
-(void)picturesDidStartLoading {
if (loadingActivity == nil)
loadingActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:loadingActivity];
[loadingActivity setCenter:[self.view center]];
[loadingActivity startAnimating];
}
-(void)picturesDidFinishLoading {
if (loadingActivity != nil) {
[loadingActivity stopAnimating];
[loadingActivity removeFromSuperview];
}
[self.tableView reloadData];
}
Now for the problem... every single time, in the listening class, the method (void)picturesDidFinishLoading is called. The method (void)picturesDidStartLoading never is called.
When I debug the code, in the resource class, the line
if ([delegate respondsToSelector:#selector(picturesDidStartLoading)])
[delegate picturesDidStartLoading];
never reaches the delegate method call - even if I remove the if statement. The line
if ([delegate respondsToSelector:#selector(picturesDidFinishLoading)])
[delegate picturesDidFinishLoading];
is always called.
any ideas?
Ok -- I figured it out....
The delegate was nil during the first call. The reason it is nil is because the function using the delegate was called in the source during the init method. The init method was not complete when the first test of the delegate was performed. At this time the delegate was nil because it is not instantiated until the the init method completes. The reason the second test of the delegate worked is because I submitted the process using an NSOperationQueue.
To fix the problem I have to move things around a bit... it's all about the timing!
Well now that was fun....
That's weird, try to remove #optional in the protocol declaration, and see if you get some warnings.
Try to print a log inside the method as well, other than that it looks fine.