Delegate TabBar from custom UITableViewCell - ios

I have created a custom cell with a specific list of properties, including UITabBar. This cell has its specific Class called RecomandationCell, where I have declared these properties.
I use this custom cell to created multiple cells with different objects, also I want to know when I click an item of this tab bar in a specific row call its method that comes from Delegation of <UITabBarControllerDelegate> which is
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item.tag==0)
{
NSLog(#"Likes Clicked");
}
if (item.tag==1)
{
NSLog(#"Comments Clicked");
}
if (item.tag==2)
{
NSLog(#"Shares Clicked");
}
if (item.tag==3)
{
NSLog(#"Add Clicked");
}
if (item.tag==4)
{
NSLog(#"Ratings Clicked");
}
}
The problem is that this method won't be fired because I don't know where to delegate it, and even If I delegate it I don't know which tab bar at a specific index was clicked.
RecomandationCell.h
#import <UIKit/UIKit.h>
#interface RecomandationCell : UITableViewCell <UITabBarControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *wineImage;
#property (weak, nonatomic) IBOutlet UITextView *wineName;
#property (weak, nonatomic) IBOutlet UILabel *wineYear;
#property (weak, nonatomic) IBOutlet UITabBar *socialTabBar;
#end
RecomandationCell.m
#import "RecomandationCell.h"
#implementation RecomandationCell
#synthesize socialTabBar;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item.tag==0)
{
NSLog(#"Likes Clicked");
}
if (item.tag==1)
{
NSLog(#"Comments Clicked");
}
if (item.tag==2)
{
NSLog(#"Shares Clicked");
}
if (item.tag==3)
{
NSLog(#"Add Clicked");
}
if (item.tag==4)
{
NSLog(#"Ratings Clicked");
}
}
#end
RecomandationViewController.h
#import <UIKit/UIKit.h>
#import "RecomandationCell.h"
#interface RecomandationViewController : UIViewController <UITableViewDataSource,UITableViewDataSource,RecommandationCellDelegate>
#property (weak, nonatomic) IBOutlet UISegmentedControl *segmentView;
#property (weak, nonatomic) IBOutlet UITableView *winesTable;
#end
RecomandationViewController.m
#import "RecomandationViewController.h"
#import "RecomandationCell.h"
#interface RecomandationViewController ()
#end
#implementation RecomandationViewController
{
NSMutableArray *rowArray;
//Titles for wine properties
NSMutableArray *wineNames;
NSMutableArray *wineProductors;
NSMutableArray *winePlaces;
NSMutableArray *wineYears;
NSMutableArray *wineRatings;
//Badges for Tab Bar items
NSMutableArray *nrOfRatings;
NSMutableArray *nrOfLikes;
NSMutableArray *nrOfComments;
NSMutableArray *nrOfShares;
NSMutableArray *addToWishLists;
}
#synthesize winesTable,segmentView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
rowArray = [[NSMutableArray alloc]initWithObjects:#"picture1.jpg",#"picture2.jpg",#"picture3.jpg",#"image4.jpeg",#"image5.jpeg",#"image6.jpeg", nil];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
// return rowArray.count;
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"mainCell";
RecomandationCell *cell = [self.winesTable dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.wineImage.contentMode = UIViewContentModeScaleAspectFill;
cell.wineImage.image = [UIImage imageNamed:[rowArray objectAtIndex:indexPath.row]];
[[cell.socialTabBar.items objectAtIndex:0]setBadgeValue:#"2"];
[[cell.socialTabBar.items objectAtIndex:1]setBadgeValue:#"3"];
[[cell.socialTabBar.items objectAtIndex:2]setBadgeValue:#"4"];
[[cell.socialTabBar.items objectAtIndex:4]setBadgeValue:#"19"];
cell.wineYear.text = #"2014";
cell.wineName.text = #"Alex\nMandi\nTirana\nOK";
return cell;
}
#end
Any suggestions or if you need more information don't hesitate to comment

oky u can do like this
in CustomCell.h
#interface CustomTabedCell : UITableViewCell<UITabBarDelegate>//confirms to delegate
in CustomCell.m file
#import "CustomTabedCell.h"
#implementation CustomTabedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:#"1" image:nil tag:10];//you can specify image hear i put nil
UITabBarItem *secondItem = [[UITabBarItem alloc]initWithTitle:#"2" image:nil tag:11]; // ... give tag number
UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 0, 320, 70)]; //leave this if u are added in xib
tabBar.delegate = self; //delegate to custom cell itself
tabBar.tag = 100;//leave
tabBar.items = [NSArray arrayWithObjects:firstItem,secondItem, nil];//add to tab bar
[self.contentView addSubview:tabBar];
}
return self;
}
//in custom cell u are getting the delegate call's
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item.tag==10) //compare using tages as u added to tabed bar
{
NSLog(#"Likes Clicked");
}
if (item.tag==11)
{
NSLog(#"Comments Clicked");
}
if (item.tag==12)
{
NSLog(#"Shares Clicked");
}
if (item.tag==13)
{
NSLog(#"Add Clicked");
}
if (item.tag==14)
{
NSLog(#"Ratings Clicked");
}
}
after this u can call custom delegate method to controller for further processing

First: You have specified UITabBarControllerDelegate in the interface for the cell, when you probably wanted to specify UITabBarDelegate.
Second: You set the delegate when you create the tabbar. Your provided code does not show this. You need to init the tabbar somewhere, and then you set a delegate on it.
Third: The place to setup something specific in a cell would be in the cellForRowAtIndexPath in the tableviewcontroller.
Fourth: To get the index of the tapped tabbar-item, you can do that in the didSelectItem method:
NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
NSLog(#"Tab index = %u", indexOfTab);

Related

Array not being passed from tableview to view controller through Delegate

I am trying to copy the array from tableview controller to view controller. I have checked the code multiple times and it seems to be okay.
//Delegate class
#import <UIKit/UIKit.h>
#protocol Category <NSObject>
#required
-(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory;
#end
#interface Categories : UIViewController <UITableViewDelegate,UITableViewDataSource>
#property (nonatomic) id<Category> delegate;
#property (nonatomic,strong) NSArray *sports;
#property (strong, nonatomic) IBOutlet UITableView *tableview;
#property (nonatomic,strong) NSMutableArray *selectedIndexes;
#end
//Delegate methods
#import "Categories.h"
#interface Categories ()
{
NSMutableArray *array ;
}
#end
#implementation Categories
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_sports= [[NSArray alloc] initWithObjects: #"Baseball", #"Soccer", #"Hockey",
#"Other",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return _sports.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
array = [[NSMutableArray alloc]init];
// Configure the cell...
cell.textLabel.text=[self.sports objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
{ [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[array addObject:cellText];
}else if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryCheckmark){
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryNone;
[array removeObject:cellText];
}
}
- (IBAction)doneButton:(id)sender {
[self.delegate delegateMethodForCategory:array];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
#import <UIKit/UIKit.h>
#import "Categories.h"
#interface ActivityCreator : UIViewController <UIPopoverPresentationControllerDelegate, Category>
#property (nonatomic) Categories *requestClass;
#property (nonatomic,strong) NSMutableArray *arrayOfSports;
#end
//This class implements delegate
import "ActivityCreator.h"
#interface ActivityCreator ()
#end
#implementation ActivityCreator
- (void)viewDidLoad {
[super viewDidLoad];
[self settingUp];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
-(void)settingUp{
_requestClass = [[Categories alloc]init];
self.requestClass.delegate = self;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"hubabuba"]){
Categories *pop = (Categories *)segue.destinationViewController;
pop.modalPresentationStyle = UIModalPresentationPopover;
pop.popoverPresentationController.delegate = self;
}
}
-(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory {
_arrayOfSports = arrayOfCategory;
NSLog(#"%#",_arrayOfSports);
}
Any guidance where I am doing wrong will be of great help. Have been stuck on this for a while.
The delegate method is not being called at all.
Thanks
Set the delegate of Categories class in prepareForSegue method instead of setting in settingUp method.
Write
pop.delegate = self;
In prepareForSegue method.

UITableView section header clickable Delegate not Call

I am new to ios development. I am create app in Storyboard, Initially UItableviewController with one prototype cells, with button and labels. The button has IBAction method in it UItableViewCell Class, the class has Delegate to UItableviewController,the delegate method does not called. But the Images in section view is change. The i post my full code here.
Download whole project from Link
ContactHeaderViewCell.h
#protocol SectionClick;
#protocol SectionClick <NSObject>
#optional
- (void) TickCheckbox : (NSInteger) section;
- (void) UnTickCheckbox : (NSInteger) section;
#end
#interface ContactHeaderViewCell : UITableViewCell
{
id<SectionClick> HeaderDelegate;
}
#property (strong, nonatomic) IBOutlet UILabel *lblName;
#property (strong, nonatomic) IBOutlet UIButton *btnCheckBox;
#property (strong, nonatomic) IBOutlet UIButton *btnArrow;
- (IBAction)btnCheckBox_click:(id)sender;
- (IBAction)btnArrow_Click:(id)sender;
#property (nonatomic, strong) id<SectionClick> HeaderDelegate;
#property (nonatomic) NSInteger section;
- (void) setDelegate:(id)delegate;
#end'
ContactHeaderViewCell.m
#implementation ContactHeaderViewCell
#synthesize HeaderDelegate,section;
- (void) setDelegate:(id)delegate
{
self.HeaderDelegate = delegate;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)btnCheckBox_click:(id)sender {
self.btnCheckBox.selected = !self.btnCheckBox.selected;
if (self.btnCheckBox.selected)
{
if ([self.HeaderDelegate respondsToSelector:#selector(UnTickCheckbox:)])
{
[self.HeaderDelegate UnTickCheckbox:self.section];
}
} else
{
if ([self.HeaderDelegate respondsToSelector:#selector(TickCheckbox:)])
{
[self.HeaderDelegate TickCheckbox:self.section];
}
}
}
ContactTableViewController.m
#import "ContactDetail.h"
#import "ContactHeaderViewCell.h"
#interface ContactTableViewController : UITableViewController<SectionClick>
#property(nonatomic) ContactDetail *contacts;
#property(nonatomic) NSMutableArray *ContactList;
#end
ContactTableViewController.m
#import "ContactTableViewController.h"
#import <AddressBook/AddressBook.h>
#import "ContactHeaderViewCell.h"
#import "UserDetailViewCell.h"
#interface ContactTableViewController ()
#end
#implementation ContactTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[self ArrayContactFunc];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.ContactList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
self.contacts =(self.ContactList)[section];
return (self.contacts.Isopen) ? [self.contacts.mobileNumbers count] : 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *HeaderIdentifier = #"HeaderCell";
self.contacts = (self.ContactList)[section];
ContactHeaderViewCell *HeaderView = (ContactHeaderViewCell *)[self.tableView dequeueReusableCellWithIdentifier:HeaderIdentifier];
if (HeaderView == nil){
[NSException raise:#"headerView == nil.." format:#"No cells with matching CellIdentifier loaded from your storyboard"];
}
HeaderView.lblName.text = self.contacts.fullName;
if(self.contacts.IsChecked)
{
[HeaderView.btnCheckBox setImage:[UIImage imageNamed:#"Unchecked.png"] forState:UIControlStateSelected];
}
else
{
[HeaderView.btnCheckBox setImage:[UIImage imageNamed:#"Checked.png"] forState:UIControlStateSelected];
}
return HeaderView;
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.contacts = (self.ContactList)[indexPath.section];
static NSString *DetailCellIdentifier = #"DetailCell";
UserDetailViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if(!cell)
{
[NSException raise:#"headerView == nil.." format:#"No cells with matching CellIdentifier loaded from your storyboard"];
}
cell.lblDetail.text = (self.contacts.mobileNumbers)[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60.0;
}
-(void)UnTickCheckbox:(NSInteger)section
{
// self.contacts = (self.ContactList)[section];
// self.contacts.IsChecked = NO;
// [self.tableView reloadData];
}
-(void)TickCheckbox:(NSInteger)section
{
// self.contacts = (self.ContactList)[section];
// self.contacts.IsChecked = YES;
// [self.tableView reloadData];
}
Thank in advance.
You must add this line
[HeaderView setHeaderDelegate:self];
in method:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

How do you load video from an array in table detail view when the user clicks on the cell?

I have loaded a table view with NSArray data. Now, I want to play a video in a detail view. The video played depends on what cell the user clicks.
How do I test the cell clicked and load a video from the array based on that?
I am using Xcode 5 for iOS 7.
Here is my ViewController.m
#import "ViewController.h"
#import "VideoDetailViewController.h"
#interface ViewController ()
{
NSMutableArray * titlearray;
NSMutableArray * arrayVidSrc;
}
#end
#implementation ViewController
#synthesize mytableview;
- (void)viewDidLoad
{
[super viewDidLoad];
//sets delegate methods of table view
self.mytableview.delegate=self;
self.mytableview.dataSource=self;
//assigns values to objects in array's
titlearray = [[NSMutableArray alloc]initWithObjects:#"intro", #"skating",nil];
arrayVidSrc = [[NSMutableArray alloc]initWithObjects:#"Step1-Intro.mp4", #"Step2-Skating.mp4", nil];
}
// returns one section in the table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//counts the items in the title array
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titlearray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"vidCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath: indexPath];
cell.textLabel.text = [titlearray objectAtIndex:indexPath.row];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetailsSeg"]) {
NSIndexPath *indexpath =nil;
NSString *titlestring =nil;
indexpath = [mytableview indexPathForSelectedRow];
titlestring = [titlearray objectAtIndex:indexpath.row];
NSURL * movieURL = [arrayVidSrc objectAtIndex:indexpath.row];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[[player view] setFrame: [self.view bounds]];
[self.view addSubview: [player view]];
[player play];
[[segue destinationViewController] setTitlecontents:titlestring];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here is my ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
//allows us to use the delegate methods of table view
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *mytableview;
#end
I have a custom view controller class VideoDetailViewController.m:
#import "VideoDetailViewController.h"
#interface VideoDetailViewController ()
#end
#implementation VideoDetailViewController
#synthesize arrayVidSrc = _arrayVidSrc;
#synthesize titlelabel;
#synthesize navBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.titlelabel.text = self.titlecontents;
self.navBar.title = self.titlecontents;
//video load from array
// NSURL * movieURL = [_arrayVidSrc objectAtIndex:NSIndexPath.row];
//Play the movie now
/*MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
and the .h file that goes with it:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UINavigationItem *navBar;
#property (weak, nonatomic) IBOutlet UILabel *selectedRow;
#property (strong, nonatomic) NSString * titlecontents;
#property (weak, nonatomic) IBOutlet UILabel * titlelabel;
#property (strong, nonatomic) NSArray *arrayVidSrc;
#end
Implement the method - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. The sender is the cell that was clicked. You can then call something like [self.tableView indexPathForCell:sender] to get the cell address and use the row of the returned index path to pick the video out of your array.
The new view controller that will be shown when the segue finishes is available within the prepareForSegue:: method as [segue destinationViewController] and you can pass it whatever it will need.

Having trouble passing NNString label name from MasterView to DetailView

I'm trying to make a Master-Detail app that displays a list of Sounds on the master view. When you tap the sound, you go to the detail screen, and it displays the Sound you chose as a label, then shows a short movie of the sound. My problem is, all the sounds show as the label the name of the first sound in the list. When I tap on Sound B, the detail screen shows "Sound B." When I tap on tSound F, the detail screen shows "Sound B."
No errors or warnings come up. Any ideas?
Thanks in advance.
slfViewController.h
#import <UIKit/UIKit.h>
#interface slfViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#end
slfViewController.m
#import "slfViewController.h"
#import "slfSoundDetailViewController.h"
#interface slfViewController ()
#end
#implementation slfViewController
{
NSArray *tableData;
NSArray *thumbnails;
}
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:
#"B Sound",
#"Ch Sound",
#"D Sound",
#"F Sound",
...
#"Zh Sound",
nil];
thumbnails = [NSArray arrayWithObjects:
#"b.jpg",
#"ch.jpg",
#"d.jpg",
#"f.jpg",
...
#"zh.jpg",
nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showSoundDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
slfSoundDetailViewController *destViewController = segue.destinationViewController;
destViewController.soundName = [tableData objectAtIndex:indexPath.row];
}
}
#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"soundCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
#end
slfSoundDetailViewController.h
#import <UIKit/UIKit.h>
#interface slfSoundDetailViewController : UIViewController
#property (nonatomic, strong) IBOutlet UILabel *soundLabel;
#property (nonatomic, strong) NSString *soundName;
#end
slfSoundDetailViewController.m
#import "slfSoundDetailViewController.h"
#interface slfSoundDetailViewController ()
#end
#implementation slfSoundDetailViewController
#synthesize soundLabel;
#synthesize soundName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
soundLabel.text = soundName;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
You’re only setting soundLabel.text = soundName in viewDidLoad… so it’s only being called once (when the view controller’s view is first loaded). This app is most likely reusing the same instance of slfSoundDetailViewController so it doesn’t reload its view.
You’ll probably want to override either -[slfSoundDetailViewController setSoundName:] or -[slfSoundDetailViewController viewWillAppea:] and set soundLabel.text there.

UITableViewDelegate not being called

I am trying to call a detail screen from a UITableView list - but the delegate is not being called in the receiving view - I'll post all the code:
list header file:
#import <UIKit/UIKit.h>
#import "tank.h"
#class iTanksV2ListViewController;
#protocol iTanksV2ListViewControllerDelegate
- (void) iTanksListViewController:(iTanksV2ListViewController *) sender choseTank:(tank *)tank;
#end
#interface iTanksV2ListViewController : UITableViewController
#property (nonatomic, strong) NSArray *tanks;
#property (weak, nonatomic) IBOutlet UITableView *tankTableView;
#property (weak, nonatomic) id <iTanksV2ListViewControllerDelegate> delegate;
#end
and the m file:
#import "iTanksV2ListViewController.h"
#import "tank.h"
#import "tankDetailViewController.h"
#interface iTanksV2ListViewController ()
#end
#implementation iTanksV2ListViewController
#synthesize tanks = _tanks;
#synthesize tankTableView = _tankTableView;
#synthesize delegate = _delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
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
{
[self setTankTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;//keep this section in case we do need to add sections in the future.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.tanks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Tank List Table Cell";
UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
cell.textLabel.text = thisTank.tankNumber;
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"Show Tank Details"])
{
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
[self.delegate iTanksListViewController:self choseTank:thisTank];
}
#end
and the header for the receiving file:
#import <UIKit/UIKit.h>
#import "tankGauge.h"
#import "tank.h"
#interface tankDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *tankNumberLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankProductLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankAvailableProductLabel;
#property (weak, nonatomic) IBOutlet UILabel *tankMaxVolumeLabel;
#property (weak, nonatomic) IBOutlet tankGauge *tankVolumeGauge;
#property (weak, nonatomic) tank* tankToShow;
#end
...and the m file:
#import "tankDetailViewController.h"
#import "iTanksV2ListViewController.h"
#interface tankDetailViewController () <iTanksV2ListViewControllerDelegate>
#end
#implementation tankDetailViewController
#synthesize tankNumberLabel = _tankNumberLabel;
#synthesize tankProductLabel = _tankProductLabel;
#synthesize tankAvailableProductLabel = _tankAvailableProductLabel;
#synthesize tankMaxVolumeLabel = _tankMaxVolumeLabel;
#synthesize tankVolumeGauge = _tankVolumeGauge;
#synthesize tankToShow = _tankToShow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)iTanksListViewController:(iTanksV2ListViewController *)sender choseTank:(id)tank
{
self.tankToShow = tank;
self.tankNumberLabel.text = self.tankToShow.tankNumber;
}
- (void)viewDidUnload
{
[self setTankNumberLabel:nil];
[self setTankProductLabel:nil];
[self setTankAvailableProductLabel:nil];
[self setTankMaxVolumeLabel:nil];
[self setTankVolumeGauge:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
tankTableView is an IBOutlet, so you just need to connect your tableView's delegate and data-source to your File's Owner in your xib as shown below:
Have you set that viewcontroller as the delegate for that tableview?

Resources