Can I Add a custom Delegate to a Custom CALayer - iphone

I am trying to add a delegate to a CALayer so I can know when the animation sequence is complete. It's animating 40 sublayers around the screen. Once I add the delegate as I do below, all the animation stops on the CALayer.
Here is my code:
#protocol NIArticlesLayerDelegate;
#interface NIArticlesLayer : CALayer {
id<NIArticlesLayerDelegate> delegate;
}
#property (nonatomic, assign) id<NIArticlesLayerDelegate> delegate;
#end
#protocol NIArticlesLayerDelegate <NSObject>
#optional
-(void)itemAtCenter:(id)item;
#end

Checking the CALayer Docs, CALayer already has a property called delegate which MUST be assigned to the owning view. Try changing the name?

Related

UIScrollView subclass and custom delegate: detect scroll events on the subclass and on the delegate

I'm creating an UIScrollView subclass. On this subclass, I need to detect the scroll events, but I also want to enable the detection of scroll events on a delegate. Also, this UIScrollView subclass needs a custom delegate.
// CustomScrollView.h
#import <UIKit/UIKit.h>
#import "CustomScrollViewDelegate.h"
#interface CustomScrollView : UIScrollView <UIScrollViewDelegate> {
...
}
#property (nonatomic, assign) id <DAGridViewDelegate> delegate;
...
#end
// CustomScrollView.m
#import "DAGridView.h"
#implementation DAGridView
#synthesize delegate;
- (id)init {
self = [super init];
if (self) {
...
}
return self;
}
...
#end
// CustomScrollViewDelegate.h
#class CustomScrollViewDelegate
#protocol CustomScrollViewDelegate <NSObject, CustomScrollViewDelegate>
...
#end
Thanks for helping!!
If you need some more information, comment!!
well, you can simply state in the init
self.delegate = self; // to have your custom scroll view as its own delegate
and rename your custom delegate of the new protocol you are implementing as CustomDelegate to avoid problem
then you must ovveride each method the scroll view calls and in each method add something like
[customDelegate ovveriddenScrollviewDelegateMethod];
The you implement in your class which will be the customDelgate,
<UIScrollViewDelegate, CustomScrollViewDelegate>
and implement enverything.

How to use delegate methods to pass objects between two view controllers?

I'm using the Utility Application project to Xcode 4.2, then I am adding two text fields for each view: main view and the flipside view.
Now I wish to assign the value of the flipside text field to mainview text field.
MainViewController.h
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UITextField *nameField;
}
#property(nonatomic, retain) IBOutlet UITextField *nameField;
- (IBAction)showInfo:(id)sender;
#end
FlipsideViewController.h
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController {
UITextField *changeText;
}
#property (nonatomic, retain) IBOutlet UITextField *changeText;
#property (assign, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
FlipsideViewController.m
#import "FlipsideViewController.h"
#implementation FlipsideViewController
#synthesize delegate = _delegate;
#synthesize changeText;
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
When the done action starts, i want the changetext value be assigned to nameField text.
How do I do this?
In MainViewController.m, implement the flipSideViewControllerDelegate method as:
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
self.nameField.text=controller.changeText.text;
}
So when the done: method is called, this delegate method is also called with your flipSideViewController object as the argument, through which changeText can be accessed.
EDIT to answer question in comment:
In your protocol FlipSideViewControllerDelegate, add this method:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
Then it is similar to the other delegate method implementation in MainViewController.m
, which is how the protocol works really. If your MainViewController conforms to the protocol, it can implement methods of that protocol. By default all the methods declared in the protocol are optional, but you have the option to specify if the method is optional or required by using
#optional
//list of methods
#required
//list of methods
Bear in mind that if your a method is declared as required in the protocol, any class conforming to that protocol must implement it. Anyway, in your MainViewController.m:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
{
int anInt=indexPath.row;
self.nameField.text=[NSString stringWithFormat:#"%d",anInt];
}

I am losing my default UIScrollViewdelegate methods when I subclass it

I try to subclass the UScrollview but it ends up losing the default UIScrollview delegate method.
#import <UIKit/UIKit.h>
#protocol myscrollviewDelegate <NSObject>
-(void) myscrollview_return;
#end
#interface myscrollview : UIScrollView <UIScrollViewDelegate> {
id<myscrollviewDelegate> delegate;
}
#property(nonatomic, assign) id<myscrollviewDelegate> delegate;
#end
(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
never get called when scroll.
what's wrong? Can I subclass the UIScrollview and add additonal delegate and at the same times keeping the original delegates??
You are not adding a property, but overriding it, as UIScrollView already has a delegate property. When you set a delegate using the new property, the reference will be stored in the instance variable you added, not in the private instance variable of the original UIScrollView.
My theory is that the implementation of UIScrollView accesses the instance variable without using the property. I haven't verified it, but try not adding a new ivar and overriding the delegate property.
You can do this without creating a second delegate property.
First, make your delegate protocol inherit from UIScrollViewDelegate:
#protocol myscrollviewDelegate <NSObject, UIScrollViewDelegate>
Then, declare the delegate property in your header for your class:
#interface myscrollview : UIScrollView <UIScrollViewDelegate>
#property(nonatomic, assign) id<myscrollviewDelegate> delegate;
And the key is to not synthesize the property, but rather make it dynamic in your implementation file.
#implementation myscrollview
#dynamic delegate;
...
This is because You implement the delegate methods with id id delegate; I hope so
so change the name of delegate. instead using delegate use other name like "delegateSomeClass" etc
Now the delegates method of UIscrollView calls
hope it will clear :)

How to correctly detect touches in my UIView?

I'm having a problem. I have a UIView, that looks like this :
In that view controller I implemented the "touchesBegan:withEvent:" method, but the method is only getting triggered when I touch the bar at the bottom of the view , nothing happens when I touch the table.
How could I change this behavior to be able to detect the touches that occur in the table too ?
This is the code that basically declares that UIViewController:
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface TripSearchDetailViewController : UIViewController
{
//Parent of this sub-view.
UIViewController *parentController;
//GUI elements
UITableView *tableView;
UIButton *backButton;
}
//Button actions
- (IBAction) goBack:(id) sender;
- (IBAction) showDatePicker:(id) sender;
//Class Methods.
- (void) presentDatePicker;
#property (nonatomic, retain) UIViewController *parentController;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) IBOutlet UIButton *backButton;
#end
It looks like you've got a UITableViewController, not a UIViewController. Your table view is intercepting and handling touches before they make it up to your view controller. You'll need to subclass UITableView, override it's -touchesBegan:withEvent: method, and then create a standard UIViewController that adds your new subclass to the view hierarchy.
I think it could possibly help to implement the method hitTest:withEvent: in your TripSearchDetailViewController. If you just return true in this method your touch should be recognized.
see also: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html

Passing object from controller to a view

I'm following iPhone dev courses from Stanford Open-University, and I've been blocked for 2 days on assignment3, maybe someone can help me here?
The tasks are:
Create a custom UIView subclass that will display your PolygonShape object
Give your view class access to the PolygonShape object so that it can retrieve the details of the polygon as needed
The problem is: how do I give my view class access to the polygon object defined in my controller?
Here is my implementations if it can help:
CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
Controller.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"
#interface Controller : NSObject {
IBOutlet UIButton *decreaseButton;
IBOutlet UIButton *increaseButton;
IBOutlet UILabel *numberOfSidesLabel;
IBOutlet PolygonShape *polygon;
IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
#end
And after you figure it out, it might not hurt to touch up on some objective-c basics:
http://www.cocoacast.com/?q=node/103
Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...
in CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
#property (readwrite, assign) PolygonShape *polygon;
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
in CustomView.m:
#implementation CustomView
#synthesize polygon;
...
#end
in Controller.m:
- (void)awakeFromNib {
// configure your polygon here
polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
[polygonView setPolygon:polygon];
NSLog (#"My polygon: %#", [polygon description]);
}
I just finished assignement 3 last night. I solved this connection all in Interface Builder. First I created an outlet on the "PolygonView" UIView subclass for the PolygonShape and then connected it to the instance of the Polygon model. From what I have read in the Google Group and on various other sites, I do not think there is one right way to connect this UIView to the model and the controller. But it worked I think there is nothing wrong with the View knowing about the model.
So why aren't you declaring them as properties of the class?