Hi in this code the protocol RefreshLibraryDropBoxDelegate doesn't work, doesn't call the method refreshLibrary in the WVdALibraryDocumentViewController. Why?
WVdADropboxViewController.h:
#import <UIKit/UIKit.h>
#import <DropboxSDK/DropboxSDK.h>
#protocol RefreshLibraryDropBoxDelegate <NSObject>
#optional
-(void)refreshLibrary;
#end
#interface WVdADropboxViewController : UIViewController <DBRestClientDelegate, UITableViewDataSource, UITableViewDelegate>
{
id <RefreshLibraryDropBoxDelegate> delegate;
}
//delegate
#property (assign) id <RefreshLibraryDropBoxDelegate> delegate;
WVdADropboxViewController.m:
- (void)restClient:(DBRestClient *)client loadedFile:(NSString *)destPath
{
NSLog(#"upload complete");
[self.delegate refreshLibrary];
[[self navigationController] popViewControllerAnimated:YES];
}
WVdALibraryDocumentViewController.h:
#import <UIKit/UIKit.h>
#import "WVdACustomCell.h"
#import "WVdAViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "WVdADropboxViewController.h"
#interface WVdALibraryDocumentViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, RefreshLibraryDropBoxDelegate>
-(void)refreshLibrary;
WVdALibraryDocumentViewController.m:
// REFRESH LIBRARY //
-(void)refreshLibrary
{
NSLog(#"refresh");
[self getDataArrayDocumentFiles];
}
You do not need this line
{
id <RefreshLibraryDropBoxDelegate> delegate;
}
Then, in WVdALibraryDocumentViewController
you need to set in viewDidLoad, or somewhere else
WVdADropboxViewController *myCoolController = [WVdADropboxViewController new];
myCoolController.delegate = self;
It should work!
I guess you for get to set the delegate inside your WVdALibraryDocumentViewController.m.
In your viewDidLoad in WVdALibraryDocumentViewController:
wVdADropboxViewController.delegate = self;
Related
i have first display start_game screen than after when i click button that time display popupview using xib.in xib class i have create delegate method.when i close the popupview that time call delegate method but not calling
here is my delegate class
.h File
#import
#protocol digbuttonalertdelegate;
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
.m File
#import "digbuttonalert.h"
#import "suggestion_alert.h"
#import "UIViewController+CWPopup.h"
#import "zoom_alert.h"
#interface digbuttonalert ()
{
bool status;
}
#end
#implementation digbuttonalert
- (void)viewDidLoad {
[super viewDidLoad];
status=0;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[self.bg_image setUserInteractionEnabled:YES];
[self.bg_image addGestureRecognizer:singleTap];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender {
}
-(void)tapDetected{
NSLog(#"single Tap on imageview");
if(status==0)
{
self.lbl_title.text=#"As you walk you will discover the hidden map.This circle will show your progress.";
status=1;
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.delegate respondsToSelector:#selector(digalertclose)]) {
[self.delegate digalertclose];
}
}
}
here this class i want to call method
#import "digbuttonalert.h"
#interface start_games () <MJSecondPopupDelegate,digbuttonalertdelegate>
{
- (void)viewDidLoad {
digbuttonalert *next=[[digbuttonalert alloc]init];
next.delegate=self;
next.modalTransitionStyle=UIWebPaginationModeRightToLeft;
next.modalPresentationStyle=17;
[self presentViewController:next animated:YES completion:nil];
}
- (void)digalertclose
{
[self StartTimer];
[[NSUserDefaults standardUserDefaults]setObject:#"false" forKey:#"alertstatus"];
}
Change .m file with below :
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
And follow basic tutorial steps : enter link description here
Please implement digalertclose delegate in start_games controller like this
-(void)digalertclose {
}
You make digalertclose optional thats why crash does not occur if you remove optional keyword from digbuttonalertdelegate protocol you can see that there will be a crash because you try to fire a delegate but does not implement it.
In your digbuttonalert.h protocol is defined twice.
There is no #end for the interface.
Should be something like
ClassA.h
#protocol YourProtocol <NSObject>
#optional
-(void)yourProtocolOptionalMethod
#end
#interface ClassA.h
#property (weak, nonatomic) id <YourProtocol> delegate;
#end
ClassA.m
-(void) someClassAMethod {
[self.delegate yourProtocolOptionalMethod];
}
ClassB.h
#import "ClassA.h"
#interface ClassB <YourProtocol>
#end
ClassB.m
-(void) someClassBMethod {
ClassA *classA = [ClassA alloc] init];
classA.delegate = self;
}
After setting the delegate now when you will call the delegate from ClassA then it will trigger the protocol implemented method in ClassB
-(void)yourProtocolOptionalMethod {
NSlog(#"");
}
On your class , you need to declare the delegate like this:
#interface YourViewController () < digbuttonalertdelegate >
I have this delegate that is not working out as planned, I have it setup like so. I want to call the function NSLog(show); I am not too sure why this isn't work, but works with my other viewcontroller. I must be missing some small detail.
AccountViewController.h
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController{
}
#property (nonatomic, assign) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.m
-(void)showLabel {
NSLog(#"SHOW");
}
you haven't shown where you assign the MapViewController to be the delegate of the AccountViewController. Perhaps thats what you are missing
//(from somewhere in the MapViewController)
AccountViewController *accountVC = //however you instantiate it (segue, storyboard etc
accountVC.accountViewDelegate = self;
Kindly note that delegate should not have strong reference.
So use
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
Now in MapViewController.m or MapViewController.h conform to this protocol as
#interface MapViewController : UIViewController <AccountViewControllerDelegate>{
}
Then in MapViewController.m please do
AccountViewController *accountVC = [[AccountViewController alloc]init]; // initialize it with whatever be like storyboard or nib
accountVC.accountViewDelegate = self;
Your AccountViewController.h file should look like this
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
#interface AccountViewController : UIViewController{
}
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
#end
Here what you should have in your different files:
AccountViewController.h
Note that "assign" is mainly for primitives like NSInteger, CGFloat, BOOL (that is, for properties that are not objects). Use "weak" to keep a pointer toward your delegate without incrementing his retain count.
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController
#property (nonatomic, weak) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
Here, it is usually good practice to check if the delegate isn't nil and if it has the method you want to call implemented. Use respondToSelector for that.
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate && [self.accountViewDelegate respondsToSelector:#selector(showLabel)]) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.h
Be sure to import "AccountViewController.h" here.
#import "AccountViewController.h"
#interface MapViewController : UIViewController <AccountViewControllerDelegate>
#end
MapViewController.m
Put the following where you were instantiating your AccountViewController object:
//Somewhere
AccountViewController *accountViewController = [[AccountViewController alloc] init];
accountViewController.accountViewDelegate = self;
and this is your delegate's method implementation:
- (void)showLabel {
NSLog(#"SHOW");
}
I'm having strange problem, trying to call my delegate - it never gets called. As i understand ,delegate never gets set. i'm using ARC.
CueCreateDelegate.h
#import <Foundation/Foundation.h>
#class CueCreateDelegate;
#protocol CueCreDelegate <NSObject>
- (void)CueCreatedCall;
#end
#interface CueCreateDelegate : NSObject
#property (nonatomic, assign) id <CueCreDelegate> delegate;
-(void)CueCreated;
- (void) setdelegate:(id<CueCreDelegate>) delegates; //for testing
#end
CueCreateDelegate.m
CueCreated is called from another class and it prints the bad news...
#import "CueCreateDelegate.h"
#implementation CueCreateDelegate
#synthesize delegate ;
- (void) setdelegate:(id<CueCreDelegate>) delegates{
delegate = delegates;
}
-(void)CueCreated{
if ([delegate respondsToSelector:#selector(CueCreatedCall)]) {
[delegate CueCreatedCall];
}
else{
NSLog(#"Delegate method not getting called...%#",delegate);
}
}
#end
MatrixViewController.h
#import <UIKit/UIKit.h>
#import "CueCreateDelegate.h"
#interface MatrixViewController : UIViewController<UIGestureRecognizerDelegate,CueCreDelegate>{
CueCreateDelegate *cueCre;
}
MatrixViewController.m
CueCreatedCall is never called..
-(void)CueCreatedCall{
NSLog(#"lsakdlakjdlks");
}
- (void)viewDidLoad
{
cueCre = [[CueCreateDelegate alloc] init];
// [cueCre setdelegate:self];
[cueCre setDelegate:self];
NSLog(#"%#",[cueCre delegate]);
}
I've a main class where I want to define two protocols (1 used by a class A, the other by class B) (ios 6.1, xcode 4.6.3 , ARK mode, storyboard project).
According to official syntax, all my code seems to be correct.
But when I try to use the second delegate , nothing work correctly, my 2nd delegate does not respond
**HEADER myProtocols.h**
#import ...
#class myProtocols;
#protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
#end
#protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
#end
#interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
#property (nonatomic, assign) id <myProtocol1> myDelegate1;
#property (nonatomic, assign) id <myProtocol2> myDelegate2;
#end
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
#end
**HEADER classA.h**
#import "myProtocols.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
#interface classB: UIViewController <myProtocol2>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
#end
**MESSAGES classB.m**
#import "classB.h"
#interface classB ()
#end
#implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(#" YES VALUE IS %f and text %# and type %#",myValue2,myText,myType);
}
So, my mistake is to call [_myDelegate2 doOtherStuff..] directly inside a function in myProtocols called only by classA.
Then, if I want to call a function in myProtocols using both delegate I MUST init both these delegates in the class (A or B doesnt matter) I use to call this function:
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
-(void) pleaseDoIt
{
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE NOW WORK VERY WELL**
}
#end
**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
#property(strong, nonatomic) classB *classBVC;
//-(void) doStuff:(float) myValue;
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_classBVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
_myProtoVC.myDelegate2 = _classBVC // THIS IS THE POINT!!!
[_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
So I have 3 views as follows: viewController >> viewController2 >> viewController3.
In viewController3 I have created a Delegate Protocol. The protocol method is a simple method that prints out an NSLog.
When I call the delegates from ViewController3, only its parent (viewController2 ) responds not the (first) viewController. There are no errors.I think problem has got something to do with [v2 setDelegate:self]; in the viewController.m file.
Nevertheless,[self.v3 setDelegate:self]; works fine in ViewController2.m file.
Why does the (first) viewController delegate not respond ? Do delegates only work with its immediate child ??
> **ViewController.h**
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#import "ViewController2.h"
#interface ViewController : UIViewController <PassData>{
ViewController2 *v2;
}
#property (strong, nonatomic) ViewController2 *v2;
> Blockquote
- (IBAction)button:(id)sender;
#end
> **ViewController.M**
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize v2;
- (IBAction)button:(id)sender {
v2 = [[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[v2 setDelegate:self];
[self.view addSubview:v2.view];
}
-(void)print: (BOOL)success;{
if (success == YES) {
NSLog(#"ViewController called");
}
}
#end
> > ViewController2.h
#import <UIKit/UIKit.h>
#import "ViewController3.h"
#interface ViewController2 : UIViewController <PassData> {
ViewController3 *v3;
}
#property (strong, nonatomic)ViewController3 *v3;
#property (retain) id delegate;
- (IBAction)button:(id)sender;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
#synthesize v3,delegate;
- (IBAction)button:(id)sender {
v3 = [[ViewController3 alloc]initWithNibName:#"ViewController3" bundle:nil];
[self.v3 setDelegate:self];
[self.view addSubview:v3.view];
}
-(void)print: (BOOL)success;{
if (success == YES) {
NSLog(#"ViewController2 called");
}
}
#end
> ViewController3.h
#import <UIKit/UIKit.h>
#protocol PassData <NSObject>
#required
-(void)print:(BOOL)success;
#end
#interface ViewController3 : UIViewController {
id<PassData> delegate;
}
#property (retain) id delegate;
- (IBAction)callButton:(id)sender;
#end
ViewController3.m
#import "ViewController3.h"
#interface ViewController3 ()
#end
#implementation ViewController3
#synthesize delegate;
- (IBAction)callButton:(id)sender {
// call all delegates
[[self delegate]print:YES];
}
#end
v2 doesn't have a method "print", that's a protocol method of v3 -- you can't chain delegate messages like this. If you want multiple controllers to respond to something in another controller, then you should use an NSNotification -- any number of objects can register to receive a notification.