delegate method does not get called - objective-c

I'm completely stuck with calling a method from a UIView subclass, the method just doesn't get fired, I have a feeling that I'm doing something wrong but after searching the web I did not find any clue. Thank you in advance
Here's the iPadMainViewController.h file
#import <UIKit/UIKit.h>
#import "TouchView.h"
#interface iPadMainViewController : UIViewController <TouchViewDelegate>
#property (retain) UIWebView *detailsView;
#end
and the iPadMainViewController.h file that holds the method
- (void)MethodNameToCallBack:(NSString *)s
{
NSLog(#"%#",s);
}
Here's the TouchView.h file, which is supposed t
#protocol TouchViewDelegate
- (void)MethodNameToCallBack:(NSString *)s;
#end
#interface TouchView : UIView {
id<TouchViewDelegate> delegate;
}
#property (nonatomic, assign) id delegate;
#end
Here's the TouchView.m file which is supposed to call a method of it's delegate
#implementation TouchView
#synthesize delegate;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"HELLO FROM INSIDE");
[[self delegate] MethodNameToCallBack:(NSString *)#"HELLO FROM OUTSIDE"];
}
#end

Synthesizing the delegate is not enough, because it just creates the getter and the setter methods. It does not create an instance of iPadMainViewController.
So after you create an instance of TouchView, you should assign an instance of iPadMainViewController as the delegate.
iPadMainViewController *controller = [[iPadMainViewController alloc] init...
// ...
TouchView *touchView = [[TouchView alloc] init...
// ...
touchView.delegate = controller;
Or in the iPadMainViewController's viewDidLoad method:
- (void)viewDidLoad
{
// ...
self.touchView.delegate = self;
}

check after you instantiated a TouchView instance, did you assign its delegate?

Enhance your touchesBegan implementation a little for further debugging:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"HELLO FROM INSIDE");
NSLog(#"our delegate is set towards: %#", delegate);
...
}
Does it log something useful in that second logging statement?
I presume it prints nil and that would be the root cause of your issue; you forgot to assign the delegate.

Related

delegation and passing data back from childViewController

I have been struggling with this for a few days and have received valuable help on the way from S.O. I have made the simplest possible project to reduce the possibilities of it being a typo.
All my project is, is a ViewController that holds a container view hooked to a childViewController. The "parent" ViewController is set as the delegate of the childViewController. In the viewDidLoad of the child I am passing a value which is just a string. This string should be passed on to the parent and printed on the console. Here are the files.
ViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
#interface ViewController : UIViewController <ChildViewControllerDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property NSString *myValueRetrieved;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ChildViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildVC"];
controller.delegate = self;
NSLog(#"Here is my value: %#",self.myValueRetrieved);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void) passValue:(NSString *)theValue{
self.myValueRetrieved = theValue;
}
#end
ChildViewController.h
#import <UIKit/UIKit.h>
#protocol ChildViewControllerDelegate;
#interface ChildViewController : UIViewController
#property (weak)id <ChildViewControllerDelegate> delegate;
#end
#protocol ChildViewControllerDelegate <NSObject>
- (void) passValue:(NSString*) theValue;
#end
ChildViewController.m
#import "ChildViewController.h"
#interface ChildViewController ()
#property NSArray *colors;
#end
#implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.delegate passValue:#"Hello"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
Am I right to think that when the app is launched, the console should log the following message: "here is my value: hello". Am I doing something wrong in terms of logically not getting delegation or is it just a silly typo somewhere? tx
You're assuming that the view is loaded when the view controller is instantiated. That's now how it works. The view gets loaded when it's needed (like to add to the parent view).
But you can force the view to load and make this work. Call -loadViewIfNeeded on the child view controller right after setting the delegate. That will probably get you what you want:
controller.delegate = self;
[controller loadViewIfNeeded];
NSLog(#"Here is my value: %#",self.myValueRetrieved);
Or, if you do want to call back the delegate in viewDidLoad, then you'd need to move the NSLog to the -passValue: method, since the primary view controller's viewDidLoad method will have already finished running.
To do this make ParentController a delegate of ChildController. This allows ChildController to send a message back to ParentController enabling us to send data back.
For ParentController to be delegate of ChildController it must conform to ChildController's protocol which we have to specify. This tells ParentController which methods it must implement.
In ChildController.h, below the #import, but above #interface you specify the protocol.
#class ChildController;
#protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item;
#end
next still in the ChildController.h you need to setup a delegate property and synthesize in ChildController.h
#property (nonatomic, weak) id <ChildControllerDelegate> delegate;
In ChildController we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack = #"Pass this value back to ParentController";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
That's it for ChildController. Now in ParentController.h, tell ParentViewController to import Child and conform to its protocol.
import "ChildController.h"
#interface ParentController : UIViewController
In ParentController.m implement the following method from our protocol
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item
{
NSLog(#"This was returned from ChildController %#",item);
}
The last thing we need to do is tell ChildController that ParentController is its delegate before we push ChildController on to nav stack.
ChildController *ChildController = [[ChildController alloc] initWithNib:#"ChildController" bundle:nil];
ChildController.delegate = self
[[self navigationController] pushViewController:ChildController animated:YES];

How to effectively call properties in another class - Objective-C

I am a beginner with Objective-C and iOS development, and I can't figure out how to send data from one class to another without it returning null or in this case "0.0000"
I'll give the code I have-
In ClassA.h I have two properties
#interface ClassA : UIView
// This is public property!
#property (nonatomic, assign) CGFloat touchPointX;
#property (nonatomic, assign) CGFloat touchPointY;
// My test getters
- (CGFloat) touchLocationX;
- (CGFloat) touchLocationY;
#end;
Then in ClassA.m I implement these.
#implementation ClassA
- (void)baseInit {
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:self.frame];
if (self) {
[self baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self];
// Save the touch locations in our local variables.
self.touchPointX = touchLocation.x;
self.touchPointY = touchLocation.y;
[self setNeedsDisplay];
}
}
- (CGFloat) touchLocationX {
return self.touchPointX;
}
- (CGFloat) touchLocationY {
return self.touchPointY;
}
#end
Then I try to log the values in AppDelegate using some code I found on here
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, copy) ClassA *touchLocationX;
#property (nonatomic, copy) ClassA *touchLocationY;
#end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
NSLog(#"(%#, %#)", _touchLocationX, _touchLocationY");
});
return YES;
}
I haven't been able to figure out how to do something like:
ClassA * myView = [[ClassA alloc] init];
myView = [myViewController theUIViewForClassA];
In the long run all I want is the properties to return the correct values
There are several ways in which data can be shared across an application.
In your example, you want to "send" touchpoints from ClassA to some other class, in this case the AppDelegate, which is fine as it demonstrates how this can work with other classes.
To accomplish this, several design patterns come to mind: Delegate Protocol, Notifications or Singleton.
I'll use the example of a singelton for convenience, because it allows us to use your AppDelegate, which is already a singleton, so this example can closely resemble your existing code.
Note that a singleton design pattern is one in which a singleton class is instantiated only once, whose methods and properties can be accessed by other classes throughout the application.
Example:
AppDelegate.h
Add a method for receiving touch events
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
- (void) notifyTouchEvents:(CGFloat)touchX :(CGFloat)touchY;
#end
AppDelegate.m
Implement the method
#implementation AppDelegate
// Method for receiving touch event data from another class
- (void) notifyTouchEvents:(CGFloat)touchX :(CGFloat)touchY
{
NSLog(#"touchX: %f, touchY: %f", touchX, touchY);
}
ClassA.h
Import the App Delegate
Note: This example doesn't require touchPointX and touchPointY properties.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface ViewController : UIViewController
#property (nonatomic, assign) CGFloat touchPointX;
#property (nonatomic, assign) CGFloat touchPointY;
#end
ClassA.m
Access the AppDelegate touch notification method during touch events
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self.view];
// Access the AppDelegate singleton and notification method
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate notifyTouchEvents:touchLocation.x :touchLocation.y];
}
}
#end
In practice, you may find that a combination of using NSNotificationCenter and a Singleton can works best. Search around for more information on using delegates and singletons with Objective C.

How to correctly implement a NSWindowController subclass with xib-file

I am trying to implement a NSWindowController subclass with new xib-file, I read up in lots of books, and researched on StackOverflow, but none of the steps provided made my window show, nor did the subclass code get executed. The new xib-file has its File's Owner set to "LogNavigatorController" and connections to the window and its contents have been made.
My AppDelegate.h:
#import <Cocoa/Cocoa.h>
#class LogNavigatorWindowController;
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
LogNavigatorWindowController *logsWindowController;
}
#end
My AppDelegate.m:
#import "AppDelegate.h"
#import "LogNavigatorWindowController.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
logsWindowController = [[LogNavigatorWindowController alloc] initWithWindowNibName:#"LogNavigatorWindowController"];
[logsWindowController showWindow:self];
}
#end
My LogNavigatorWindowController.h:
#import <Cocoa/Cocoa.h>
#interface LogNavigatorWindowController : NSWindowController
{
NSArray *directoryList1;
NSArray *directoryList2;
NSMutableArray *directoryList;
NSMutableArray *filePaths1;
NSMutableArray *filePaths2;
}
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTableView *logsTableView;
#property (unsafe_unretained) IBOutlet NSTextView *logsTextView;
#property (assign) IBOutlet NSArrayController *LogListController;
#property (retain) NSMutableArray *logsArray;
- (void) myDirectoryLogFunction;
#end
My LogNavigatorController.m:
#import "LogNavigatorWindowController.h"
#interface LogNavigatorWindowController ()
#end
#implementation LogNavigatorWindowController
#synthesize logsTableView;
#synthesize logsTextView;
#synthesize window;
- (id)init
{
self = [super initWithWindowNibName:#"LogNavigatorWindowController"];
[self loadWindow];
[self showWindow:#"Log Navigator"];
[self.window makeKeyAndOrderFront:nil];
if (self)
{
// Initialization code here.
[self myDirectoryLogFunction];
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
- (void) myDirectoryLogFunction
{
NSLog(#"Code execution test successful");
}
#end
You don't need to create the window property since it is already available for NSWindowController subclasses. Maybe that causes the problem.
Also your init method contains a lot of code that doesn't belong there. Remove
[self loadWindow];
[self showWindow:#"Log Navigator"];
[self.window makeKeyAndOrderFront:nil];
as well as replace
self = [super initWithWindowNibName:#"LogNavigatorWindowController"];
with
self = [super init];
You may want to remove the init method at all, since you don't need it in your case.
and move
[self myDirectoryLogFunction];
to the windowDidLoad method.
Also always check that the code for instantiating the window controller (in your case from the app delegates didFinishLaunching: ) is called. Sometimes it helps to create a new project and test there, if you may have changed too much within the original project and by accident removed delegate connections or similar.

delegate method not getting called

I have looked at all the other questions with the same problem but I cannot seem to get my heard around it. I am pretty sure I have done everything correctly as this is not my first time using delegates.
//PDFView.h
#class PDFView;
#protocol PDFViewDelegate <NSObject>
-(void)trialwithPOints:(PDFView*)pdfview;
#end
#interface PDFView : UIView
#property (nonatomic, weak) id <PDFViewDelegate> delegate;
In the implementation file i am trying to call the delegate method from touchesMoved delegate of view
//PDFView.m
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate trialwithPOints:self];
}
The class where the delegate method is implemented
//points.h
#import "PDFView.h"
#interface points : NSObject <PDFViewDelegate>
//points.m
//this is where the delegate is set
- (id)init
{
if ((self = [super init]))
{
pdfView = [[PDFView alloc]init];
pdfView.delegate = self;
}
return self;
}
-(void)trialwithPOints:(PDFView *)pdf
{
NSLog(#"THE DELEGATE METHOD CALLED TO PASS THE POINTS TO THE CLIENT");
}
So this is how i have written my delegate and somehow the delegate is nil and the delegate method is never called.
At the moment I am not doing anything with the delegate, I just want to see it working.
Any advices on this would be highly appreciated.
I think it is because you did not hold the reference to the instance of the delegate, and it got released because it is declared weak. You might be doing this:
pdfView.delegate = [[points alloc] init];
which you should fix to something like:
_points = [[points alloc] init];
pdfView.delegate = _points;
where _points is instance variable.

Delegate - How to Use?

I think I understand the logic behind a delegate. I got more the problem to use it. How many steps are involved? Do I have to use existing delegates? Or can I use my one ones?
In my example I got the AppDelegate that created many views (Objects / View Controllers) of the same type. Each view should somehow call a method on the AppDelegate to close itself. This would happen when a button within the view is touched. The method call would include the reference of the view (self).
So far I know from other languages responders, event listeners and so on. They are so simple to use.
Can anybody help me. I just found massive examples with a lot of code in the web. It can't be that hard to just call a parent in Objective C.
I think you should use for this the NSNotificationCenter
in you AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(buttonPushed:) name:#"ButtonPushedNotification" object:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
...
...
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
this is the selector it will be called when the notification happens (we are still in the AppDelegate.m)
- (void)buttonPushed:(NSNotification *)notification {
NSLog(#"the button pushed...");
}
and in the ViewController.m when the button pushed (inside the method), you should post a notification like this:
{
...
[[NSNotificationCenter defaultCenter] postNotificationName:#"ButtonPushedNotification" object:nil];
...
}
You can create your own:
In MyView1.h:
#class MyView1;
#protocol MyView1Delegate <NSObject>
- (void)closeMyView1:(MyView1 *)myView1;
#end
#interface MyView1 : NSObject
{
id<MyView1Delegate> _delegate;
}
#property (assign, nonatomic, readwrite) id<MyView1Delegate> delegate;
...
#end
In MyView1.m:
#interface MyView1
#synthesize delegate = _delegate;
...
// The method that tells the delegate to close me
- (void)closeMe
{
....
if ([_delegate respondsToSelector:#selector(closeMyView1:)])
{
[_delegate closeMyView1:self];
}
}
#end
In AppDelegate.h:
#import "MyView1.h"
#interface AppDelegate <MyView1Delegate>
{
MyView1 *_myView1;
}
...
#end
In AppDelegate.m:
- (void)someCreateViewMethod
{
_myView1 = [[MyView1 alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[_myView1 setDelegate:self];
...
}
An easy way to get what you want is to just start with one view. Then, have each other view be presented modally. When the button in the view is pressed do
[self dismissModalViewControllerAnimated:YES];
And here's something I made a while ago when I was starting iPhone development that might help you with delegates
Delegates
//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"segueName"])
{
childController *foo = segue.destinationViewController;
foo.delegate = self;
}
}
//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
//An example of what you could do if your data was an NSDate
buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}
//In parent .h file:
//import child header
#import "ChildName.h"
//indicate conformity with protocol
#interface ParentName : UIViewController <ChildNameDelegate>
//In child .h file
//declare protocol
#protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
#end
//declare delegate
#property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;
//In child .m file
//synthesize delegate
#synthesize delegate;
//use method
- (IBAction)actionName:(id)sender
{
[delegate methodName:assignedData];
}