Delegate method not working ios - iphone

I used delegate mehod for pass data between view controllers. This is not working.
#protocol PassCountry <NSObject>
#required
- (void) setPickedCountry:(NSString *)pickedCountry;
#end
#interface SelectCountryViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> {
id <PassCountry> delegate;
}
#property (copy) NSString *pickedCountry;
#property (retain) id delegate;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent (NSInteger)component {
pickedCountry = [self.countries objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.countries.count;
}
- (void)viewWillDisappear:(BOOL)animated {
[[self delegate] setPickedCountry:pickedCountry];
}

#import <UIKit/UIKit.h>
#protocol PassCountry <NSObject>
#required
- (void) setPickedCountry:(NSString *)pickedCountry;
#end
#interface secondViewViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
{
id <PassCountry> delegate;
IBOutlet UIButton *aButton;
}
#property (copy) NSString *pickedCountry;
#property (assign) id<PassCountry> delegate; // for delegate use assign don't retain
// in another class you are creating instance of this class
secondViewViewController *secController = [[secondViewViewController alloc]init];
secController.delegate = self;//check this added or not
[self presentViewController:secController animated:YES completion:nil];
//and implementation of deleagte method
- (void) setPickedCountry:(NSString *)pickedCountry
{
// do some stuff
}

Firstly, delegate instance can not be retained.
Secondly, delegate should be synthesized using "#synthesize delegate" before invoke the method [self delegate].

Try this::
.h File
#protocol delegateTextSize <NSObject>
#optional
-(void)selectedTextSize:(double)textSize;
#end
#interface CustomFontSizeCell : UITableViewCell
#property (nonatomic,retain) id delegateTextSize;
-(IBAction)changeSize:(id)sender;
#end
.m File
-(IBAction)changeSize:(id)sender
{
[delegateTextSize selectedTextSize:app.selectedFontSize];
}
Where to use,
.h File
Controller <delegateTextSize>
.m File
-(void)selectedTextSize:(double)textSize
{
}
Hopefully, this will work
Thanks.

Related

Protocol method does not get invoked, shows delegate 'nil'

I'm working on an iPad App and i'm having issue with delegate... the protocol method does not get invoked. i'm not sure what i'm missing, here is my code.
#protocol pickerLabelProtocol <NSObject>
- (void)selectedPickerData:(UILabel *)sender;
#end
#interface showPickerVC : UIViewController
#property (nonatomic, strong) id <pickerLabelProtocol> delegate;
#end
#implementation showPickerVC
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//i used breakpoint, the 'delegate' is always nil for some reason?
[self.delegate selectedPickerData:self.mainLabel];
}
----------------------
#interface someViewController : UIViewController <pickerLabelProtocol>
#property (nonatomic, strong) showPickerVC *showPicker;
#end
#implementation someViewController
- (void)selectedPickerData:(UILabel *)sender
{
//protocol method
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.showPicker = [[showPickerVC alloc]init];
self.showPicker.delegate = self;
}
I can not got any mistake From your code but i suggest you that Be clear about when you create object of showPickerVC add it's delegate self
Such Like ,
showPickerVC *obj = [[showPickerVC alloc] init];
obj.delegate = self; /// YOur protocol delegate
.
.
[self presentModalViewController:obj animated:YES];
And Also add code as following
#implementation showPickerVC
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if([self.delegate respondsToSelector:#selector(selectedPickerData:)])
{
[self.delegate selectedPickerData:self.mainLabel];
}
}
For More information about How to create/use of Protocol.

Protocol Delegate Implementation not working

I am implementing a Protocol of my UIViewController Class. I need to perform methods of this Protocol in another class. But its not working.
Below is the code. Please help me out.
///This is my Header Class
#import <UIKit/UIKit.h>
#protocol PauseViewDelegate <NSObject>
- (IBAction)ResumeButtonPressed:(id)sender;
- (IBAction)MenuButtonPressed:(id)sender;
- (IBAction)RestartButtonPressed:(id)sender;
#end
#interface PauseViewController : UIViewController
#property (nonatomic, retain) id<PauseViewDelegate> pauseDelegate;
#end
////This is implementation class protocol Methods.
#import "PauseViewController.h"
#interface PauseViewController ()
#end
#implementation PauseViewController
#synthesize pauseDelegate;
- (IBAction)ResumeButtonPressed:(id)sender
{
NSLog(#"resume delegate");
[pauseDelegate ResumeButtonPressed:sender];
}
- (IBAction)RestartButtonPressed:(id)sender
{
NSLog(#"Restart delegate");
[self.view removeFromSuperview];
[pauseDelegate RestartButtonPressed:sender];
}
- (IBAction)MenuButtonPressed:(id)sender
{
NSLog(#"Menu delegate");
[self.view removeFromSuperview];
[pauseDelegate MenuButtonPressed:sender];
}
And i am using these methods in different view controller but its not working.

custom viewcontroller delegate not working

i'm having issues on my custom delegate, not sure why is not working.i wan to pass the delegate to the mainview so it will auto push to a detail view after adding a new record.
Not sure what i have missed out.i'm doing all the linkage with storyboard & iOS 5.
thanks for looking and appreciated all comments
addnote.h
#import <UIKit/UIKit.h>
#import "memoView.h"
#protocol addNoteDelegate;
#interface addNote : UIViewController
{
IBOutlet UITextField *memoNameTextField;
IBOutlet UITextField *memoCommentsTextField;
id <addNoteDelegate> delegate;
}
#property (nonatomic,assign) id <addNoteDelegate> delegate;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
#end
#protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
#end
addnote.m
-(IBAction)add:(id)sender
{
[[self delegate] addNoteDidFinish:self];
}
mainview.h
#import <UIKit/UIKit.h>
#import "addNote.h"
#interface mainView : UITableViewController<NSFetchedResultsControllerDelegate, addNoteDelegate>
{
}
#property (nonatomic, retain) NSArray *memoInfo;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSManagedObjectContext *ObjectContext;
-(IBAction)backToMain:(id)sender;
- (IBAction)addNote:(id)sender;
#end
mainView.m
-(IBAction)addNote:(id)sender
{
addNote *addingNote = [[addNote alloc] init];
addingNote.delegate = self;
[self performSegueWithIdentifier:#"addNote" sender:self];
}
- (void)addNoteDidFinish:(addNote *)controller{
//PUSH TO NEXT VIEW
[[self navigationController] dismissModalViewControllerAnimated:YES];
}
#protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
#end
#interface addNote : UIViewController
{
IBOutlet UITextField *memoNameTextField;
IBOutlet UITextField *memoCommentsTextField;
id <addNoteDelegate> delegate;
}
#property (nonatomic,assign) id <addNoteDelegate> delegate;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
#end
and:
[self dismissModalViewControllerAnimated:YES];
not:
[[self navigationController] dismissModalViewControllerAnimated:YES];
Seems I was missing a function:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"addNote"]) {
UINavigationController *navController = [segue destinationViewController];
addNote *addingNote = (addNote *)navController.topViewController;
addingNote.delegate = self;
}
}

Custom delegate not working

I have started working more with delegate as suggested in another question I made. Now, I have made a UIViewController called ProfileViewController in which I would like to load the News Feed from Facebook. I also have subclass of NSObject called FBFeed. This subclass loads the Facebook News Feed and should pass it back to the view controller. All my requests to Facebook are sent through a singleton named FBRequestWrapper which (in this case) should pass the result to FBFeed.
I call getFBRequestWithGraphPath:andDelegate: in FBRequestWrapper which will call a method in the Facebook iOS SDK which takes a delegate as a parameter. If I put in self (which will be the FBRequestWrapper) it works just fine. If I put in _delegate (which is an instance of FBFeed) the application crashes with EXC_BAD_ACCESS.
I have a theory why it might crash. I have read that
#property (nonatomic, retain) id <FBFeedDelegate> delegate;
should be
#property (nonatomic, assign) id <FBFeedDelegate> delegate;
But when doing so I get the following error:
Existing ivar 'delegate' for unsafe_unretained property 'delegate'
must be __unsafe_unretained
Also, I don't know if that enough to make the application crash.
Here is my code.
ProfileViewController.h
#import <UIKit/UIKit.h>
#import "FeedTableView.h"
#import "FBFeed.h"
#interface ProfileViewController : UIViewController <FBFeedDelegate>
{
FeedTableView *tableView;
}
- (void)loadFeed;
#end
ProfileViewController.m
#implementation ProfileViewController
- (void)loadFeed
{
FBFeed *feed = [[FBFeed alloc] init];
[feed loadNewsFeedWithDelegate:self];
}
#pragma mark -
#pragma FBFeedDelegate
- (void)finishedLoadingFeed:(FBFeed *)_feed
{
NSLog(#"ProfileViewController: FBFeed Finished.");
}
- (void)failedToLoadFeed:(FBFeed *)_feed
{
NSLog(#"ProfileViewController: FBFeed Failed.");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Load feed
[self loadFeed];
}
#end
FBFeed.h
#import <Foundation/Foundation.h>
#import "FBRequestWrapper.h"
#protocol FBFeedDelegate;
#interface FBFeed : NSObject <FBRequestDelegate>
{
id <FBFeedDelegate> delegate;
}
#property (nonatomic, retain) id <FBFeedDelegate> delegate;
- (void)loadNewsFeedWithDelegate:(id)_delegate;
#end
#protocol FBFeedDelegate <NSObject>
#required
- (void)finishedLoadingFeed:(FBFeed *)_feed;
- (void)failedToLoadFeed:(FBFeed *)_feed;
#end
FBFeed.m
#import "FBFeed.h"
#implementation FBFeed
#synthesize delegate;
- (void)loadNewsFeedWithDelegate:(id)_delegate
{
self.delegate = _delegate;
[[FBRequestWrapper defaultManager] getFBRequestWithGraphPath:#"me" andDelegate:self];
}
#pragma mark -
#pragma mark FBRequest Delegate
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"FBFeed: FBRequest Did Load.");
NSLog(#"%#", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"FBFeed: FBRequest Failed.");
NSLog(#"%#", error);
}
#end
FBRequestWrapper.h
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#interface FBRequestWrapper : NSObject <FBRequestDelegate, FBSessionDelegate>
{
Facebook *facebook;
BOOL isLoggedIn;
}
#property (nonatomic, assign) BOOL isLoggedIn;
+ (id)defaultManager;
- (void)setIsLoggedIn:(BOOL)_loggedIn;
- (void)FBSessionBegin:(id<FBSessionDelegate>)_delegate;
- (void)FBLogout;
- (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate;
- (void)getFBRequestWithMethodName:(NSString *)_methodName andParams:(NSMutableDictionary *)_params andDelegate:(id)_delegate;
#end
FBRequestWrapper.m
#import "FBRequestWrapper.h"
static FBRequestWrapper *defaultWrapper = nil;
#implementation FBRequestWrapper
#synthesize isLoggedIn;
+ (id)defaultManager
{
if(!defaultWrapper)
{
defaultWrapper = [[FBRequestWrapper alloc] init];
}
return defaultWrapper;
}
- (void)getFBRequestWithGraphPath:(NSString *)_path andDelegate:(id)_delegate
{
if (_path != nil)
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if (_delegate == nil)
{
_delegate = self;
}
[facebook requestWithGraphPath:_path andDelegate:_delegate];
}
}
#pragma mark -
#pragma mark FBRequestDelegate
- (void)request:(FBRequest *)request didLoad:(id)result
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"%#", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"FBRequest Failed: %#", error);
}
#end
I guess you are using ARC and the new iOS5 SDK which is still under NDA. Instead of using retain, use the Keyword 'strong'. Using strong will have the same behaviour intended as using retain. Set up your property like this:
#property (nonatomic, strong) id <FBFeedDelegate> delegate;
I think there are two ways for you:
Don't use ARC and leave your code as it was
If you want to use ARC, be aware that delegates normally shouldn't be retained. But when using ARC, an ivar declaration like this:
id <FBFeedDelegate> delegate
defaults to
id <FBFeedDelegate> __strong delegate
So when you declare a property like this:
#property (readwrite, unsafe_unretained) id <FBFeedDelegate> delegate
The ivar should look like this:
id <FBFeedDelegate> __unsafe_unretained delegate
I had the same problem but then I was able to make it work correctly under iOS 5.1 on XCode 4.3.2
Use this code in your .h file
#protocol AnimationManagerCountdownTimerDelegate <NSObject>
#required
-(void)countdownCompleted;
#end
#interface AnimationManager : NSObject{
....
}
#property (nonatomic, strong) id <AnimationManagerCountdownTimerDelegate> countdownTimerDelegate;
#end
Then in the .m file you simply synthesize:
#synthesize countdownTimerDelegate;

Implementing delegate methods for modal view controller data transfer

I have a simple project to present a modal view controller and transfer back a string based on which button in the modal VC that gets pressed. I based it all on watching the Stanford class on iTunes U. It looks like I have everything correct, but I get a couple of compiler warnings.
First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m
Second I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*' but these aren't displayed in the build results area, rather next to the offending lines in MyModalViewController.m, both lines in each of the button actions.
Here's the code...
// TransferViewController.h
#import <UIKit/UIKit.h>
#import "MyModalViewController.h";
#interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
UILabel *label;
UIButton *button;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) UIButton *button;
- (IBAction)updateText;
#end
// TransferViewController.m
#import "TransferViewController.h"
#implementation TransferViewController
#synthesize label;
#synthesize button;
- (IBAction)updateText {
MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; // I get the warning here.
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}
#end
// MyModalViewController.h
#import <UIKit/UIKit.h>
#protocol MyModalViewControllerDelegate;
#interface MyModalViewController : UIViewController {
UIButton *abby;
UIButton *zion;
id <MyModalViewControllerDelegate> delegate;
}
#property (assign) id <MyModalViewControllerDelegate> delegate;
- (IBAction)selectedAbby;
- (IBAction)selectedZion;
#end
#protocol MyModalViewControllerDelegate <NSObject>
#optional
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;
#end
// MyModalViewController.m
#import "MyModalViewController.h"
#implementation MyModalViewController
#synthesize delegate;
- (IBAction)selectedAbby {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Abby"];
}
}
- (IBAction)selectedZion {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Zion"];
}
}
Get rid of those *s after id <something> and before delegate.
So make this
id <MyModalViewControllerDelegate> *delegate;
this
id <MyModalViewControllerDelegate> delegate;