Why isn't my custom delegate working? - objective-c

I'm trying to build a delegate to allow me to pass data form a child view controller to the parent. I've been looking at various tutorials / questions online but my delegate method isn't being triggered.
Could you take a look at my code below and see if I'm missing anything?
TownListViewController.h
#protocol TownListViewControllerDelegate;
#interface TownListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
id <TownListViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <TownListViewControllerDelegate> delegate;
#end
#protocol TownListViewControllerDelegate
#optional
- (void)didSelectTown:(Town *)town;
#end
TownListViewController.m
#implementation TownListViewController
#synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
[self.navigationController popViewControllerAnimated:YES];
}
#end
SearchViewController.h
#interface SearchViewController : UITableViewController <TownListViewControllerDelegate> {
...
}
SearchViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TownListViewController * townViewController = [[TownListViewController alloc] initWithNibName:#"TownListViewController" bundle:nil];
townViewController.delegate = self;
[self.navigationController pushViewController:townViewController animated:YES];
}
- (void)didSelectTown:(Town *)town
{
NSLog(#"didSelectTown fired");
self.selectedTown = town;
}
Any help is much appreciated.

try to reloadData after set the delegate. I think the first call to reloadData of table occurs during the initWithNibName of the TownListViewController... Anyway can you post the delegate value on this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I bet is nil...
By the way is a good practice to check if the variable delegate conforms the protocol, try adding this line under supposing that the method is optional:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([delegate conformsToProtocol(TownListViewControllerDelegate)] && [delegate respondsToSelector:#selector(didSelectTown:objectAtIndexPath:)])
[delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
[self.navigationController popViewControllerAnimated:YES];
}

Related

Unselecting the table column when ever Sorting happens

I have an Application which loads an Table on click of an button, currently for the table Sorting option has been removed, But when ever I click on any column and then Click on Table Header , the selected Column should get de-selected. Not sure how to achieve this.
Thanks in Advance!!!!
You have to trigger 'buttonSelected:' method :)
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSIndexPath *currentSelectedIndexPath;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation ViewController
#pragma mark - UIViewControllerDelegate
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentSelectedIndexPath = nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentSelectedIndexPath = indexPath;
}
- (IBAction)buttonSelected:(UIButton *)sender {
[self.tableView deselectRowAtIndexPath:self.currentSelectedIndexPath animated:YES];
}
#end

Push to viewcontroller and parse data

I have a simple UITableView which is suppose to push to a new Viewcontroller. When the Cell is pressed it should push to the new viewcontroller and send what number of row has been pushed. Is there a way to obtain this?
This is what i have at the moment, which dosent do anything.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YoutubeViewController *youtubeViewController = [[YoutubeViewController alloc] initWithNibName:#"YoutubeViewController" bundle:nil];
[self.navigationController pushViewController:youtubeViewController animated:YES];
}
Do like this
This method is the delegate that will be called when a the user selects a cell in tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Write code here to push your viewcontroller
YourViewController *controller=[[YourViewController alloc]initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
controller.selectedRowValue = indexPath.row;
[self presentViewController:controller animated:YES completion:NULL];
}
In YourViewController.h
#interface YourViewController : UIViewController
#property int selectedRowValue;
#end
In YourViewController.m
#implementation YourViewController
#synthesize selectedRowValue;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Selected Row %d",selectedRowValue);
}
Check protocol of UITableViewController delegate https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.
Method You are looking for
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
yes do one thing
create property of integer type in your YoutubeViewController
like this
#property int selectedRowValue;
after that syntsize it in YoutubeViewController.m class
// YoutubeViewController.m
#implementation YoutubeViewController
#synthesize selectedRowValue
now you come class where ur table is and make some changes to it as shown bellow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YoutubeViewController *youtubeViewController = [[YoutubeViewController alloc] initWithNibName:#"YoutubeViewController" bundle:nil];
youtubeViewController.selectedRowValue=indexPath.row;
[self.navigationController pushViewController:youtubeViewController animated:YES];
}
now every thing done to check index value selected in youtube class NSLogout the value
//YoutubeViewController.m
- (void)viewDidLoad
{
// nslogout the value to check
NSLog(#"yours data selected from table%d",selectedRowValue);
}
now every thing done .
enjoy coding
Even you dont want this you can go by creating Constructor or by storing the value to NSUserDefaults

Delegate method not being called by viewController

So here's what's going on (other than me being a noob).
I have two view controllers, AddAPlaceViewController and showCategoriesViewController.
I have a table cell in AddAPlaceVC which you tap and showCategoriesVC (which is a tableviewcontroller) opens up (modal) and gives you a list of ten options. You tap one of them or tap on the cancel button.
I've spent days understanding and figuring out protocols and delegates. I think I have them set up right but can't figure out why they aren't working or getting called. I've tried all sorts of changes but no luck so far.
You guys are my only hope! :)
Here's the important bits of the code:
AddAPlaceViewController.h
AddAPlaceViewController.h
#import <UIKit/UIKit.h>
#import "showCategoriesViewController.h"
#interface AddAPlaceViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate, ShowCategoriesViewControllerDelegate>
AddAPlaceViewController.m
AddAPlaceViewController.m
#import "AddAPlaceViewController.h"
#import "FSQVenue.h"
#import "Categories.h"
//#import "showCategoriesViewController.h"
#import <QuartzCore/QuartzCore.h>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Un-highlight the selected cell
[categoryTable deselectRowAtIndexPath:indexPath animated:YES];
showCategoriesViewController *SCVC = [[showCategoriesViewController alloc] init];
SCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"showCategories"];
SCVC.categoryDelegate = self;
[self.navigationController presentModalViewController:SCVC animated:YES];
}
#pragma mark showCategoriesViewControllerDelegate
-(void)didSelectOptions:(NSInteger )selectedCategory
{
NSInteger category = selectedCategory;
NSLog(#"Add Places %d", category);
[self dismissModalViewControllerAnimated:YES];
}
-(void)didCancelOptions
{
NSLog(#"Add Places -- Dismiss Button Pressed");
[self dismissModalViewControllerAnimated:YES];
}
showCategoriesViewController.h
showCategoriesViewController.h
#import <UIKit/UIKit.h>
#protocol ShowCategoriesViewControllerDelegate;
#interface showCategoriesViewController : UITableViewController
{
id<ShowCategoriesViewControllerDelegate>categoryDelegate;
}
#property (nonatomic, weak) id<ShowCategoriesViewControllerDelegate> categoryDelegate;
- (IBAction)cancelButton:(id)sender;
#end
#protocol ShowCategoriesViewControllerDelegate <NSObject>
- (void)didSelectOptions:(NSInteger )selectedCategory;
- (void)didCancelOptions;
#end
showCategoriesViewController.m
showCategoriesViewController.m
#import "showCategoriesViewController.h"
#import "Categories.h"
#interface showCategoriesViewController ()
#property (strong, nonatomic) NSArray *categoryArray;
#end
#implementation showCategoriesViewController
#synthesize categoryDelegate = _categoryDelegate;
#synthesize categoryArray;
…
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[_categoryDelegate didSelectOptions:indexPath.row];
NSLog(#"Selected IndeXPath %#", cell);
NSLog(#"Selected IndeXPath GOING TO DELEGATE %d", indexPath.row);
}
- (IBAction)cancelButton:(id)sender {
[_categoryDelegate didCancelOptions];
NSLog(#"Button Pressed");
}
Most likely guess? In the code you posted, both your property and your ivar are called categoryDelegate, yet you treat your property as though it refers to an ivar called _categoryDelegate. Probably this is just a typo, but because iOS allows calls to methods on null objects (and just doesn't do anything), it fails silently. Renaming the ivar to _categoryDelegate should fix it.
Since you have presentModalViewController: your SCVC, your AddAPlaceViewController is inactive. Its better if you can create your showCategoriesViewController as a subclass of UITableView with your custom delegate method.
Reference Link

Master Detail Storyboard: Table View not showing cells (Objective-C)

I'm having troubles with the master viewController not showing any cells. This is the situation:
The app uses storyboard.
When the app launches, it goes to the navigationController
A button is pressed and connected to the table ViewController and it's set to "push" to it.
I've added the object and made a cell/detailView or whatever.
For some reason, the cell won't show up!!!
Here's the files:
MasterViewController.h:
#import <UIKit/UIKit.h>
#import "CraftingDetail.h"
#import "Crafting.h"
#class CraftingList;
#interface CraftingMaster : UITableViewController
#property (strong, nonatomic) CraftingDetail *detailViewController;
#property (strong, nonatomic) CraftingList *CL;
#end
MasterViewController.m:
#import "CraftingMaster.h"
#import "CraftingList.h"
#interface CraftingMaster ()
#end
#implementation CraftingMaster
#synthesize detailViewController = _detailViewController;
#synthesize CL;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.CL = [[CraftingList alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.CL.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.CL.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [self.CL craftingAtIndex:indexPath.row].Title;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
DetailViewController.h:
#import <UIKit/UIKit.h>
#interface CraftingDetail : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *Image;
#property (strong, nonatomic) IBOutlet UITextView *Description;
#end
This is an old question, but having an unexpectedly empty table view is a common issue when starting to develop with table views so hopefully this answer will be of use to someone.
Here are some things to check when your table view is devoid of cells and you expected otherwise:
Are your data source object(s) (self.CL in this case) valid? (ie. Are they != nil and point to the correct object?)
Does numberOfSectionsInTableView: return an integer value greater than zero?
Does tableView:numberOfRowsInSection: return an integer value greater than zero?
Here are a couple of problems in MasterViewController.m above that need attention:
InitWithStyle: will not be executed when the view controller is instantiated in a storyboard. Instead, initWithCoder: should be used. I suspect this was the source of JomanJi's pain as this resulted in self.CL not being instantiated. (As an aside, the data source object/property: CL should be instantiated by assigning the value to the _CL ivar directly, not to the property. See "Initializing a property, dot notation" to learn why).
Due to returning (what is likely) the same value for numberOfSectionsInTableView: as tableView:numberOfRowsInSection: (ie. "return self.CL.count;"), the table view will display the same number of sections as there are cells in each section with each sections' cells containing the same data as the other sections. I doubt this effect was what the developer intended. (This is of course unless the count accessor method in CraftingList does something really strange).
Without seeing the code for CraftingList it is impossible to determine exactly what the problem is. However, given the age of the question, I suspect JomanJi has since figured it out on his/her own.

Editable UITableView with a textfield on each cell

I am new to the iOS world and I want to know how to make a UITableView with custom cells that look and behave like the one you have when you try to configure some WiFi connexion on your device. (You know the UITableView with cells containing UITextFields with blue font where you set up the ip address and all that stuff... ).
To make a custom cell layout do involve a bit of coding, so I hope that dosen't frighten you.
First thing is creating a new UITableViewCell subclass. Let's call it InLineEditTableViewCell. Your interface InLineEditTableViewCell.h could look something like this:
#import <UIKit/UIKit.h>
#interface InLineEditTableViewCell : UITableViewCell
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UITextField *propertyTextField;
#end
And your InLineEditTableViewCell.m could look like this:
#import "InLineEditTableViewCell.h"
#implementation InLineEditTableViewCell
#synthesize titleLabel=_titleLabel;
#synthesize propertyTextField=_propertyTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Here you layout your self.titleLabel and self.propertyTextField as you want them, like they are in the WiFi settings.
}
return self;
}
- (void)dealloc
{
[_titleLabel release], _titleLabel = nil;
[_propertyTextField release], _propertyTextField = nil;
[super dealloc];
}
#end
Next thing is you set-up your UITableView as you normally would in your view controller. When doing this you have to implement the UITablesViewDataSource protocol method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. Before inserting your implementation for this, remember to #import "InLineEditTableViewCell" in your view controller. After doing this the implementation is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
InLineEditTableViewCell *cell = (InLineEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"your-static-cell-identifier"];
if (!cell) {
cell = [[[InLineEditTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"your-static-cell-identifier"] autorelease];
}
// Setup your custom cell as your wish
cell.titleLabel.text = #"Your title text";
}
That's it! You now have custom cells in your UITableView.
Good luck!