I have two UITableView, each one has a different delegate in the same UIViewController.
I'm trying to call control in another delegate, but it failed.
Brief:
mainViewController contains UITableView (with UITableViewDelegate and UITableViewDataSource) + UIImageView.
secondaryTable contains UITableView (with UITableViewDelegate and UITableViewDataSource).
I want to call UIImageView from secondaryTable.
In .h
#interface secondaryTable : UIViewController <UITableViewDataSource,UITableViewDelegate>
#end
#interface mainViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
#property (nonatomic,retain) IBOutlet UIImageView *navbar;
#property (nonatomic,retain) IBOutlet UITableView *Table1;
#property (nonatomic,retain) IBOutlet UITableView *Table2;
#end
In .m
#implementation secondaryTable
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 0) cell.textLabel.text =#"A";
if (indexPath.row == 1) cell.textLabel.text =#"B";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected");
mainViewController *myController = [[mainViewController alloc] init];
[myController.navbar setHidden:NO];
}
#end
#implementation mainViewController
- (void)viewDidLoad {
Table1.delegate = self;
Table1.dataSource = self;
secondaryTable *myDelegate = [secondaryTable alloc];
Table2.delegate = myDelegate;
Table2.dataSource = myDelegate;
#end
}
It types "Selected" in console log, but it won't show control.
Why can't it call navbar control in mainViewController?
I would suggest doing this by having both tables point to the same delegate but, within your delegate methods, select behaviour based on which table it is. Example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == firstTable)
{
// Do appropriate things here
}
else if (tableView == secondTable)
{
// do things for table #2 here
}
else
{
assert(no); // error checking
}
}
Related
I'm trying to populate the menu in ECSlidingViewController(a UITableVIEW) using the code below, but when I run the app the menu, or uitableview, is blank and not populated with the 3 MenuItems; Important, Invitation, and Agenda. I suspect the problem lies within didSelectRowAtIndexPath, but I'm not sure. Any help with this would be much appreciated. Thank you.
#import "MenuViewController.h"
#interface MenuViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation MenuViewController
#synthesize menuItems;
- (void)awakeFromNib
{
self.menuItems = [NSArray arrayWithObjects:#"Important", #"Invitation", #"Agenda", nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:#"%#", [self.menuItems objectAtIndex:indexPath.row]];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
1) Be sure to connect datasource and delegate outlets in IB, and
2) Is MenuViewController a subclass of UITableViewController? If not, then I think you're going to need to load the table data yourself someplace. Try this:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData]; // need to have an outlet named this
}
If you don't have an outlet setup called tableView, pointing to the table view, create one in IB.
I suspect that your menuItems array is empty. Try populating it in viewDidLoad.
You may want to take a look at this for a discussion of awakeFromNib, viewDidLoad, initWithNibName.
I am trying to use ECSlidingViewController which is available on github:
https://github.com/edgecase/ECSlidingViewController
Anyway I was wondering how to push data from the menu view controller to the sample table view controller because there is no segue. The navigation controller gets instantiated via storybaord but I see no way to push any data from view to another.
I tried now with delegates but it seems my delegate does not fire for some reason I dont know. The TableView stays empty. What did I miss?
Please see code below:
//MenuviewController.h
#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#protocol TableData <NSObject>
- (void) someData:(NSString*)data;
#end
#interface MenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>{
__unsafe_unretained id <TableData> delegate;
}
#property (nonatomic,assign) id <TableData> delegate;
#end
//MenuViewController.m
#import "MenuViewController.h"
#interface MenuViewController()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation MenuViewController
#synthesize menuItems, delegate;
- (void)awakeFromNib
{
self.menuItems = [NSArray arrayWithObjects:#"First", #"Second", #"Third", #"Navigation", nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:#"%#Top", [self.menuItems objectAtIndex:indexPath.row]];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
if ([delegate respondsToSelector:#selector(someData:)]) {
[delegate someData:#"Hello World"];
}
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
#end
//SampleTableViewController.h
#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#interface SampleTableViewController : UITableViewController <UITableViewDataSource, UITabBarControllerDelegate, TableData>
- (IBAction)revealMenu:(id)sender;
#end
//SampleTableViewController.m
#import "SampleTableViewController.h"
#interface SampleTableViewController()
#property (nonatomic, strong) NSArray *sampleItems;
#end
#implementation SampleTableViewController
#synthesize sampleItems;
- (void)awakeFromNib
{
//self.sampleItems = [NSArray arrayWithObjects:#"One", #"Two", #"Three", nil];
}
- (void) someData:(NSString *)data{
sampleItems = [NSArray arrayWithContentsOfFile:data];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.sampleItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"SampleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.sampleItems objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
#end
I would suggest using a delegate. Here is an example: Delegate example
I finally found out.
Here is an example.
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
MBFancyViewController *viewController = navigationController.viewControllers[0];
//or alternative
MBFancyViewController *viewController = (MBFancyViewController *)navigationController.topViewController;
// setup "inner" view controller
viewController.foo = bar;
[self presentViewController:navigationController animated:YES completion:nil];
I have 2 ViewControllers, in 1st - TableView and in 2nd - button with label on it. When I click on the button in 2nd ViewController I need to go back on TableView and set in
cell.detailTextLabel.text
text from label on the button.
Here is my code, but it does not work:
ViewController.h:
#import <UIKit/UIKit.h>
#import "TestControl.h"
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, myTestProtocol>
{
TestControl *myProtocol;
}
#property (strong, nonatomic) IBOutlet UITableView * tableTest;
#end
ViewController.m:
#import "ViewController.h"
#import "TestControl.h"
#implementation ViewController
#synthesize tableTest;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
myProtocol = [[TestControl alloc]init];
myProtocol.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TestControl * test = [[TestControl alloc] initWithNibName:#"TestControl" bundle:nil];
[self.navigationController pushViewController:test animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Firs Cell";
cell.detailTextLabel.text = myProtocol.myLabel.text;
break;
case 1:
cell.textLabel.text = #"Second Cell";
break;
case 2:
cell.textLabel.text = #"Third Cell";
break;
default:
break;
}
return cell;
}
#end
TestControl.h:
#import <UIKit/UIKit.h>
#protocol myTestProtocol <NSObject>
#end
#interface TestControl : UIViewController
{
UILabel *myLabel;
}
#property (nonatomic, assign) id <myTestProtocol> delegate;
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)testButton:(id)sender;
#end
TestControl.m:
#implementation TestControl
#synthesize myLabel;
#synthesize delegate;
- (IBAction)testButton:(id)sender
{
myLabel.text = #"TEXT LABEL";
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
What is the problem in this???
A couple of things....
You are creating two different TestControl objects, setting the delegate for one of them and pushing the other one, so the one handling the button tap has no delegate.
The delegate logic would work better the other way around. That is, TestControl should have the code that communicates with its delegate rather than the delegate "pulling" from TestControl.
I need to update image in Firstviewcontroller if cell is selected in tableviewcontroller.
My application Navigation base . Firstviewcontroller contains button event. tap on button i am firing tableview. In tableview i am selecting cell and updating checkmark in left side , It is working fine . My question is when cell is selected same time i need to update different custom image in viewcontroller for indicating cell is selected. With ref. i got image cell selecting in inside of tableview.
here it is Appdelegate
#interface FertilityAppAppDelegate : NSObject <UIApplicationDelegate>
{
Fertility *updateImage;
}
#property (nonatomic, retain) Fertility *updateImage;
#implementation
#synthesize updateImage;
#interface Fertility : UIViewController
{
UIImageView *imgView;
FertilityAppAppDelegate *Appdelegate;
}
#property(nonatomic,retain) FertilityAppAppDelegate *Appdelegate;
#property (nonatomic, retain) UIImageView *imgView;
Viewcontroller for image update
#implementation Fertility
#synthesize imgView,Appdelegate;
- (void)viewDidLoad
{
[super viewDidLoad];
Appdelegate = (FertilityAppAppDelegate*)[[UIApplication sharedApplication] delegate];
}
- (void)changeImg:(UIImage*)image
{
imgView.image = image;
}
- (void)assignObjToTableViewClass
{
Appdelegate.updateImage = self;
}
Tableview controller
#interface Menstruation : UITableViewController
{
FertilityAppAppDelegate *Appdelegate;
}
#property (nonatomic, retain) FertilityAppAppDelegate *Appdelegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [UIImage imageNamed:#"bar.png"];
[Appdelegate.updateImage changeImg:image];
selectedRow = indexPath.row;
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[[cell imageView] setImage:[UIImage imageNamed:#"Tick.png"]];
}
[[cell imageView] setHidden:YES];
if (indexPath.row == selectedRow)
{
[[cell imageView] setHidden:NO];
}
NSString *cellValue = [MenstruationArray objectAtIndex:indexPath.row];
cell.text = cellValue;
return cell;
}
How to display in Firstviewcontroller. the above image is Button with red color bar in bottom of button.
When i click this button in Firstviewcontroller firing for Menstruation table view, same time i need to update image bar to Firstviewcontroller.
if you want to change the image in the another view controller then you need to assign the image in the didSelectRowAtIndexPath method.
For that you need to access the specific view controller object(that is already using not the new one).
There are no tutorials, for these type of things.
I will send some code, please follow like that.
First of all you have to declare ImgViewController class object in the AppDelegateClass. Then only you can use.
Then use the following code. I have written this code for understanding.
#interface ImgViewController : UIViewController{
UIImageView *imgView;
AppDelegateClass *delegate;
}
#implementation ImgViewController
- (void)viewDidLoad{
delegate = (AppDelegateClass*)[[UIApplication sharedApplication]delegate];
delegate.imageViewControllerObj = self; // Or you can call the method
//[self assignObjToTableViewClass];
}
- (void)changeImg:(UIImage*)image{
imgView.image = image;
}
- (void)assignObjToTableViewClass{
delegate.imageViewControllerObj = self;
}
#end
#interface TableViewControllerClass : UIViewController{
AppDelegateClass *delegate;
}
#implementation TableViewControllerClass
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIImage *image = //Give what ever image you want to change
[delegate.imageViewControllerObj changeImg:image];
}
#end
Regards,
Satya
I have a simple UITableView, and I want to add a checkmark whenever I select a row. I have only one section in my table.
SampleTable.h
#interface SampleTable : UITableView <UITableViewDelegate , UITableViewDataSource>
{
NSMutableArray *itemArray;
NSString *itemValue;
}
#property (nonatomic,retain) NSMutableArray *itemArray;
#property (nonatomic,retain) NSString *itemValue;
-(NSMutableArray *) displayItemArray;
#end
SampleTable.m
#import "SampleTable.h"
#implementation DropTableView
#synthesize itemArray,itemValue;
-(id)initWithFrame:(CGRect)frm
{
if(self=[super initWithFrame:frm])
{
[self displayItemArray];
self.delegate=self;
self.dataSource=self;
self.separatorStyle=UITableViewCellSeparatorStyleNone;
}
return self;
}
-(NSMutableArray *) displayItemArray {
if(itemArray==nil) {
itemArray=[[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",nil];
}
return itemArray;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.itemArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
[cell autorelease];
}
[self flashScrollIndicators];
self.scrollEnabled=YES;
[self showsVerticalScrollIndicator];
cell.textLabel.text = [self.itemArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//stuff
}
-(void) dealloc
{
[itemValue release];
[itemArray release];
[super dealloc];
}
#end
See both links and then you will get a solution for this problem.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/5112-checkbox-button-iphone-application.html
http://weheartgames.com/2009/06/simple-iphone-checkbox/
I hope it will help you.