iOS Delegate not called - ios

For a project I need to be able to send a status back from "secondTableViewController" to "firstTableViewController". I followed some tutorials on delegates but I don't seem to get it to work. When I want to call the method in firstTableViewController from the secondTableViewController I do a check to see if "delegate" property is empty, and every time it is..
This is my code:
SecondTableViewController.h
#protocol SendStatusDelegate <NSObject>
#required
-(void)didStatusChanged;
#end // eof delegate protocol
#interface SecondTableViewController : UITableViewController
#property (nonatomic, assign) id<SendStatusDelegate> delegate;
#end
SecondTableViewController.m
// Status is changed on row click
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.delegate) { // This is never called because delegate looks to remain empty
[self.delegate didStatusChanged];
}
}
FirstTableViewController.h
#interface FirstTableViewController : UITableViewController <SendStatusDelegate>
#end
FirstTableViewController.m
The only thing that goes on here is implementing the -(void)didStatusChanged method.

First You need to set the delegate to self in FirstTableViewController when you init the SecondTableViewController. say on didSelectRowAtIndexPath: you are calling the SecondTableViewController in that case,
FirstTableViewController.m
// Status is changed on row click
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondTableViewController *secondTableVC = [SecondTableViewController alloc] init];
secondTableVC.delegate = self;
}

In the FirstTableViewController where you create the SecondTableViewController instance you should assign the delegate of the created instance to FirstTableViewController
ie
secondTableViewControllerInstance.delegate = self;
in FirstTableViewController

Your FirstTableViewController Class should conform the SendStatusDelegate Like
#interface FirstTableViewController : UITableViewController <SendStatusDelegate>
And then then assign the delegate of SecondViewController instance by self Like
SecondTableViewControllerInstance.delegate = self;

Related

In Split view controller: Call detailed view controller method from master view controller

In my MasterViewController uitabelview did select method change the DetailedViewController view.
My problem is when i change the view in DetailedViewController need to save the textfield values if user not saved manually.
How can i check this from MasterViewController or any idea to detect master view did select method called from DetailedViewController.
thanks in advance.
in your MasterViewController.h
#protocol CellSelectionDelegate <NSObject>
-(void)rowSelected:(NSIndexPath *)indexPath;
#end
#interface MasterViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
}
#property(nonatomic,strong)IBOutlet UITableView *menuTable;
#property (nonatomic, weak) id<CellSelectionDelegate> cellDelegate;
#end
in your MasterViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.cellDelegate respondsToSelector:#selector(rowSelected:)])
{
//sending selected indexPath, you can use indexPath.row in your detail view
[self.cellDelegate rowSelected:indexPath];
}
}
Now in your DetailedViewController.h
#import "MasterViewController.h"
#interface DetailedViewController : UIViewController<CellSelectionDelegate>
{
}
#property(nonatomic,strong)MasterViewController *masterView;
#end
in DetailedViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//Set master page as the delegate.
masterView.delegate = self;
}
Now declare following delegate method
-(void)rowSelected:(NSIndexPath *)indexPath
{
NSLog(#"Your selected Row : %d",indexPath.row);
//do your work according to selection made in master view. use indexpath.row to identify which option was selected, you can also pass the other data with rowselected: method
}

Trying to dismiss a popover view controller with a table view inside of it

I'm trying to dismiss a popover when selecting a cell inside of it.
I have created a custom delegate to support this however it is not working:
In my class that houses the PopOver and table View I have the following:
In .h:
#protocol DismissDelegate <NSObject>
-(void)didTap;
#end
#interface AssistanceNeededAtPopOverViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (nonatomic, assign) id <DismissDelegate> delegate;
In .m:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.delegate didTap];
}
In .h of the viewcontroller where the popover lives in:
#interface GlobalPageSideDetailViewController : BaseViewController <UITextFieldDelegate, UIAlertViewDelegate,UIPopoverControllerDelegate,DismissDelegate>
and in .m:
AssistanceNeededAtPopOverViewController *classpop = [[AssistanceNeededAtPopOverViewController alloc]init];
classpop.delegate = self;
-(void)didTap{
if (self.assistanceNeededAtPopover != nil) {
[self.assistanceNeededAtPopover dismissPopoverAnimated:YES ];
self.assistanceNeededAtPopover = nil;
}
}
this should be read and the popover should be dismissed...any help would be appreciated...
You have to set the delegate of your AssistanceNeededAtPopOverViewController that pops the controllerview, as the new GlobalPageSideDetailViewController.
Here you're setting the delegate of a controller you just instantiate and not the one which poped the controller.

protocol and delegate not accessing method

I am trying to pass some data back to my UIView, its abit of a complicated situation I will try to explain it.
I have my
mainViewController // request is made to RequestClass
requestClass // Request is sent off to DB for data, data is returned then passed to seriesDataClass
seriesDataClass // sorts the data and then sends back the needed info to mainViewController using protocol & delegates
This is what my code looks like for setting up the protocols and delegates
mainViewController.h
#import "SeriesDataClass.h"
#interface MatchingSeriesViewController : UIViewController <GetSeriesViewParsedData>
{
mainViewController.m
#import "SeriesDataClass.h"
// this is where I set up my protocols delegate.
- (void)viewDidLoad
{
//..
// get delegate ready
SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];
[seriesDataClass setDelegate:self];
//..
// pass data over to requestClass so you can get the info from the DB
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
[requestClass GetSeries:requestID];
//..
}
requestClass.m
// dose a bunch of stuff then calls seriesDataClass and passes all of its information over to it
//..
SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];
[seriesDataClass recivedData:uncompressedData];
//..
seriesDataClass.h
#import <Foundation/Foundation.h>
// This passes data back to the mainViewController
#protocol GetSeriesViewParsedData <NSObject>
- (void)sendGetSeriesArrays:(NSDictionary *)series LSeries:(NSDictionary *)lSeries;
#end
#interface SeriesDataClass : NSObject <NSXMLParserDelegate> {
//..
}
#property (assign, nonatomic) id <GetSeriesViewParsedData> delegate;
seriesDataClass.m
#implementation SeriesDataClass
#synthesize delegate;
// then in a method later on i call the delegate to pass the information back to mainViewController but this dosnt do anything atm.
//..
[self.delegate sendGetSeriesArrays:seriesDictionary LSeries:lSeriesDictionary];
//..
mainViewController.m
// then back in the mainViewController class I Have the method
- (void)sendGetSeriesArrays:(NSDictionary *)series LSeries:(NSDictionary *)lSeries {
// this method is never accessed
}
So my question is what am i missing in the set up of this protocol/delgate for it not to be working correctly?
I hope the structure of the question makes sense if you need any more information please let me know.
Your problem is that you are creating 2 instances of your SeriesDataClass, one in MainViewController, and another in RequestClass. So, the instance that calls the delegate method is not the same instance that the main view controller set itself as the delegate of. When you create an instance of RequestClass in MainViewController, you need to pass seriesDataClass (the instance you created) to a property in RequestClass, so that it will be working with the same instance.
You should create a property in MainViewController:
#property (strong,nonatomic) SeriesDataClass *seriesDataClass;
The viewDidLoad method should then look like this:
- (void)viewDidLoad {
self.seriesDataClass = [[SeriesDataClass alloc] init];
[self.seriesDataClass setDelegate:self];
}
Then in didSelectRowAtIndexPath, create your RequestClass instance, and pass it seriesDataClass:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RequestClass *requestClass = [[RequestClass alloc] init];
requestClass.seriesDataInstance = self.seriesDataClass;
[requestClass GetSeries:requestID];
}
In the .h of RequestClass you need to have created that property:
#property (strong,nonatomic) SeriesDataClass *seriesDataInstance;
Then in RequestClass, this:
SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];
[seriesDataClass recivedData:uncompressedData];
should be replaced with just:
[self.seriesDataInstance recivedData:uncompressedData];
I've changed some of the capitalization, to reflect the way you should do it. Classes should start with capital letters, and instances and methods should start with lowercase ones.
Change this:
#import "SeriesDataClass.h"
// this is where I set up my protocols delegate.
- (void)viewDidLoad
{
//..
// get delegate ready
SeriesDataClass *seriesDataClass = [[GetSeriesResultItem alloc] init];
[seriesDataClass setDelegate:self];

IOS: Passing value from UITableView row to UILabel in First Controller

In my second controller when user select a row and after clicked on the back button
I would like to update a UILabel with the text choice.
I have added a UILabel on the first controller and connected to h. file but I don't know how go on
I would like to replicate the GENERAL/AUTO-LOCK function in iPhone/iPad Settings.
You should use delegates for this.
SecondViewController.h:
#protocol SecondViewControllerDelegate;
#interface
...
#property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
...
#end
#protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewController:(SecondViewController *)vc didTapRowWithText:(NSString *)text;
#end
SecondViewController.m:
#implementation
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.delegate secondViewController:self didTapRowWithText:cell.yourLabel.text];
}
...
#end
whoever created and pushed SecondViewController (and maybe/hopefully also 1st one):
// put this after secondViewController initialization
secondViewController.delegate = self;
- (void)secondViewController:(SecondViewController *)vc didTapRowWithText:(NSString *)text
{
firstViewController.yourLabel.text = text;
}

Delegate not working when selecting row on UITableView

I have a UITableView, which when I select a row, I want to segue to another view, where the details of the tank are displayed... The segue happens, but the delegate does not send the object etc over... I think I have my knickers in a twist over where the delegate is declared/synthesized and implemented etc...
Here is where I declare the delegate in the header file of the UITableViewController:
#class iTanksV2ListViewController;
#protocol iTanksV2ListViewControllerDelegate
- (void) iTanksListViewController:(iTanksV2ListViewController *) sender choseTank:(tank *)tank;
#end
#interface iTanksV2ListViewController : UITableViewController
#property (weak, nonatomic) id <iTanksV2ListViewControllerDelegate> delegate;
#end
and this is what I have in the UITableViewControllers m file:
#implementation iTanksV2ListViewController
#synthesize delegate = _delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tankTableView.delegate = self;
self.tankTableView.dataSource = self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedTank = [self.tanks objectAtIndex:indexPath.row];
[self.delegate iTanksListViewController:self choseTank:self.selectedTank];
}
...and then here is the code I have for the delegate in the DetailViewController implementation file...
#interface tankDetailViewController () <iTanksV2ListViewControllerDelegate>
#end
-(void)iTanksListViewController:(iTanksV2ListViewController *)sender choseTank:(id)tank
{
self.tankToShow = tank;
}
All the other variables etc are declared, I have left them out in the hope it makes things clearer - the code compiles, and the segue happens but the delegate doesn't seem to do anything!!!
You mention segues so I'm assuming you're using a storyboard. If that's true, forget about delegates for this purpose. A delegate is supposed to be an object that performs some action for another but all you need is data transfer.
I suggest using the prepareForSeque: method instead in your iTanksV2ListViewController class. Keep the line that sets self.selectedTank when a row is tapped but then put something like this in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
tankDetailViewController *next = (tankDetailViewController *)[segue destinationViewController];
next.tankToShow = self.selectedTank;
}

Resources