Passing Multiple value to nextview controller - ios

I am developing an app in which there are three different tables and single UiView which contains label. I want to display name of that selected table-view cell name set it to the label.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate * appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if(appdelegate.btn1)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
}
else if(appdelegate.btn2)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
}
else if(appdelegate.btn3)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
}
NSString * str=[appdelegate.name objectAtIndex:indexPath.row];
NSString * str1=[appdelegate.iname objectAtIndex:indexPath.row];
NSString * str2=[appdelegate.bname objectAtIndex:indexPath.row];
DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];
[self.navigationController pushViewController:dvc animated:YES];
}
And Accessing That cell Name into init method.
-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
{
if (self)
{
self.trickname=name_;
self.trickname2=name2_;
self.trickname3=name3_;
}
return self;
}
Here trickname,trickname2,trickname3 are the NSString.
I have tried this code but not work for me.
please help me out this.

-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
{
self =[super init];
if (self)
{
self.trickname=name_;
self.trickname2=name2_;
self.trickname3=name3_;
}
return self;
}
try this this may help you. your self object not be initialized so the values are not being set in the string.

You can do this alternatively :
Make three property strings in DetailViewController.h class as :
#property (strong, nonatomic) NSString *name1;
#property (strong, nonatomic) NSString *name2;
#property (strong, nonatomic) NSString *name3;
and then syntehsize these in DetailViewController.m class as :
#synthesize name1,name2,name3;
and then access these in tableview delegate didselectrow as
DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];
dvc.name1= str;
dvc.name2= str1;
dvc.name3= str2;
[self.navigationController pushViewController:dvc animated:YES];
and then access them in your init method.
Hope it helps you.

Leave this all and do like this for passing value to Detailview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//do what you did that app delegate thing
//now we will pass value which you got from doing stuff above
//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
// i have just put DetailViewController here as nibname you give name of your detailviewcontrollers XIB
dvController.trickname = string1;
dvController.trickname2 = string2;
dvController.trickname3 =string3;
// you are passing values of string 1 2 and 3 to detail view controller string trickname, trickname2 and 3
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
NOTE: trickname,trickname2,trickname3 need to be declared in detail view controller in .h file

I solve my issue now.
Declare that string in table view controller
{
NSString * str;
NSString * str1;
NSString * str2;
}
#property(strong,nonatomic)NSString * str;
#property(strong,nonatomic)NSString * str1;
#property(strong,nonatomic)NSString * str2;
synthesize in .m of table view controller.
#synthesize str,str1,str2;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate * appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if(appdelegate.btn1)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
str=[appdelegate.name objectAtIndex:indexPath.row];
}
else if(appdelegate.btn2)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
str1=[appdelegate.iname objectAtIndex:indexPath.row];
}
else if(appdelegate.btn3)
{
appdelegate.selectId=indexPath.row;
NSLog(#"select Id %d",appdelegate.selectId);
str2=[appdelegate.bname objectAtIndex:indexPath.row];
}
DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];
dvc.trickname=str;
dvc.trickname2=str1;
dvc.trickname3=str2;
NSLog(#"%#",str);
appdelegate.counter=0;
[self.navigationController pushViewController:dvc animated:YES];
}
Then goto Detailview Controller and accessing this 3 strings through init() method of Detailview controller.
1st define that init method in DetailVewController.h
-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
#property(nonatomic,retain)NSString * trickname;
#property(nonatomic,retain)NSString * trickname2;
#property(nonatomic,retain)NSString * trickname3;
DetailViewController.m
#synthesize trickname;
#synthesize trickname2;
#synthesize trickname3;
-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
{
self=[super init];
if (self)
{
self.trickname=name_;
self.trickname2=name2_;
self.trickname3=name3_;
}
return self;
}

Related

selected row in custom cell doesn't pass complete data from segue to another view controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.data= [self.nameList objectAtIndex:indexPath.row];
self.data= [self.rollList objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"DetailVC" sender:self];
}
I've displayed student's name and roll number in custom cell. Now when i select one row i would like to pass those data to another UIViewController and display in labels. For I've one NSObject type file called 'Name' and i've one variable called 'data' which is of type 'Name'. Now here's my code in didSelectRowAtIndexPath where the nameList is overlapped by rollList which are stored on same variable self.data. if rollList line is removed then it works good. But when I store rollList in self.data it will overlap other.
Where, self.nameList is variable of type NSMutableArray which contains name of student and similarly, self.rollList contains student's roll.
Below is the code to pass data from segue. Here DetailVC is the destinationViewController, vc.data is the variable of type 'Name' and self.data is also the variable of same type.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"DetailVC"]){
DetailVC *vc = (DetailVC *) segue.destinationViewController;
vc.data = self.data;
}
}
Overlapping is fundamental thing.
In your case your in NSObject type file (Name) you have to add two variable
NSNumber *rollNo;
NSString *name;
After that
-- UPDATE --
#interface TableVC : UIViewController {
Name *data;
}
-(void)viewDidLoad {
data = [[Name alloc] init]
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.data.name = [self.nameList objectAtIndex:indexPath.row];
self.data.rollNo = [self.rollList objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"DetailVC" sender:self];
}
Then
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"DetailVC"]){
DetailVC *vc = (DetailVC *) segue.destinationViewController;
vc.data = self.data;
}
}
------ Detail VC ---
#property (nonatomic, strong) Name *data;
-(void)viewDidLoad {
self.labelRollNo.text = self.data.rollNo;
self.labelName.text = self.data.name;
}
try this way
#interface ViewController ()
{
NSString *name;
NSString *roll;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
name= [self.nameList objectAtIndex:indexPath.row];
roll= [self.rollList objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"DetailVC" sender:self];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"DetailVC"]){
DetailVC *vc = (DetailVC *) segue.destinationViewController;
vc.name = name;
vc.roll = roll;
}
}
you create two NSString name and roll properties in your DetailVC
DetailVC.h
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSString *roll;

UILabel isn't added to view

I have two viewControllers where one of them contains a UILabel and a UIButton, and the other one a UITableView. When the UIButton in the first view is pressed the new ViewController is presented and when a row in the tableview is selected the tableview is dismissed and the UILabel in the first viewController should be updated so its text is the same as the cell name tapped in the tableview.
Code:
In the first Viewcontroller:
- (void)changeTitleWithString:(NSString *)title
{
UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)];
[selected_station setFont:[UIFont systemFontOfSize:16.0]];
selected_station.textAlignment = NSTextAlignmentCenter;
selected_station.text = [NSString stringWithFormat:#"%#", title];
[self.view addSubview:selected_station];
NSLog(#"%#", selected_station);
NSLog(#"%#", title);
}
In second viewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %d", indexPath.row+1];
NewViewController *viewCOntroller = [[NewViewController alloc] init];
[viewController changeTitleWithString:self.currentString];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
The changeTitleWithString function is called and the string from tableview is successfully received in the function, but the UILabel won't show up. When I log out the UILabel (selected_station) it's not nil and its text is correct. Why doesn't the string show up on the view?
This does not look like you are referencing back to a previous allocated view controller (NewViewController). Rather you are allocating a new NewViewController.
A better approach might be to create a delegate protocol for the second view controller and make the NewViewController it's delegate --
So create a second view controller with a delegate property.
something like this
#protocol SecondViewControllerDelegate;
#interface SecondViewController : UIViewController
#property (nonatomic, strong) NSString *currentString;
#property (nonatomic, assign) id<SecondViewControllerDelegate>delegate;
#end
//----------------------------------------------------------------------------------
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string;
#end
Now in the implementation of the SecondViewController do something like this
#implementation SecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %ld", indexPath.row+1];
if ([self.delegate respondsToSelector:#selector(secondViewController:didChangeSelectionText:)]) {
[self.delegate secondViewController:self didChangeSelectionText:self.currentString];
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#end
Now in the implementation of the FirstViewController (your NewViewController) do something like this
#implementation FirstViewController
-(void) showSecondController
{
SecondViewController *viewController = [[SecondViewController alloc] init];
[viewController setDelegate:self]; // set this controller as the delegate
// add the rest of the display code here
}
#pragma mark - SecondViewControllerDelegate Methods
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string
{
//change your text here
}
#end
This should be enough to get you pointed in the right direction
In FirstViewController
- (void)changeTitleWithString:(NSString *)title :(UIView *)labelView
{
UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)];
[selected_station setFont:[UIFont systemFontOfSize:16.0]];
selected_station.textAlignment = NSTextAlignmentCenter;
selected_station.text = title;
[labelView addSubview:selected_station];
NSLog(#"%#", selected_station);
NSLog(#"%#", title);
}
InSecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %d", indexPath.row+1];
NewViewController *viewCOntroller = [[NewViewController alloc] init];
[viewController changeTitleWithString:self.currentString:self.view];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
I hope it helps

How to display data from master view into detail view controller

I am new to Ios programming i want to display the data from master view controller into the detail view controller, i am using the master detail template from xcode. the detail array is working it shows the detail in label, how ever i want to display the title as well which is stored in _objects array,
here is my code
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController () {
NSMutableArray *_detailObjects;
NSMutableArray *_thumbnailImage;
}
#end
#implementation MasterViewController
#synthesize _objects;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title= #"Zodiac List";
_objects = [[NSMutableArray alloc] initWithObjects:
#"value 1",
#"value 2",
#"value 3",
#"value 4",
#"value 5",
,nil];
_detailObjects = [[NSMutableArray alloc] initWithObjects:
#"detail of value 1",
#"detail of value 2",
#"detail of value 3",
#"detail of value 4",
#"detail of value 5",
, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSMutableArray *object = _detailObjects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
the detail is working fine how ever i want to display the value 1, value 2,value 2 in a label as well..
here is my detail view controller code..
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) id detailItemTitle;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (weak, nonatomic) IBOutlet UILabel *zodiacNameLabel;
here is .m file code
#import "DetailViewController.h"
#import "MasterViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
#synthesize zodiacNameLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
-(void) setDetailItemTitle:(id)detailItemTitle{
if (_detailItemTitle !=detailItemTitle) {
_detailItemTitle = detailItemTitle;
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
// self.zodiacNameLabel.text = [self.]
MasterViewController *master = [[MasterViewController alloc] init];
zodiacNameLabel.text = [master._objects objectAtIndex:0];
}
// if (self.detailItemTitle) {
// self.zodiacNameLabel.text = [self.detailItem description];
// }
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
// MasterViewController *master = [[MasterViewController alloc] init];
// zodiacNameLabel.text = [master._objects objectAtIndex:0];
// MasterViewController *master = [[MasterViewController alloc]init];
// zodiacNameLabel.text = [master._objects objectAtIndex:0]; // you can get first element of an array
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Implement delegate method of tableview which is in your masterviewcontroller and then inside that push your data whatever you want to display in masterviewcontroller like that:-
Assuming that below delegate is in your masterViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_yourController = [[yourController alloc] initWithNibName:#"yourController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:_yourController animated:YES];
}
Now this will alloc, initialize and load your nib into detailviewcontroller and if you want to send message from master to detail then declare property method in detailviewcontroller and just pass it from above like that below:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_yourDetailController = [[yourDetailController alloc] initWithNibName:#"yourDetailController" bundle:[NSBundle mainBundle]];
[_yourDetailController setTempString:#"test"]; //Assuming that tempString is declared in yourDetailController class
[self.navigationController pushViewController:_yourDetailController animated:YES];
}
Make an array that is "tmpArray" in detailViewController and then you can copied _objects array in to tmpArray. like this
MastrerViewController.m
DetailViewController *detail = [[DetailViewController alloc]init];
detail.tmpArray = [[NSMutableArray alloc] initWithArray:_objects];
DetailViewController.h
#property (nonatomic,retain) NSMutableArray *tmpArray;

Updating detailDescriptionLabel data from UITableView selected index

I have a split view controller for iPad with a drill-down table on the left. I am able to populate the tables with a drill-down without issue but I cannot seem to get the item I clicked within the UITableView on the left side to show up in detailDescriptionLabel on the right side.
I have the following code in my ProductViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"Row clicked: %i", indexPath.row);
if (_delegate != nil) {
Product *product = (Product *) [_products objectAtIndex:indexPath.row];
[_delegate productSelectionChanged:product];
}
DetailedVC *detailView = [[DetailedVC alloc] initWithNibName:#"DetailedVC" bundle:[NSBundle mainBundle]];
NSLog(#"User clicked on: %#", [NSString stringWithFormat:#"%d",indexPath.row]);
detailView.detailDescriptionLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
[self.navigationController pushViewController:detailView animated:YES];
}
What happens here is I push the DetailedVC into the left side where my table view is and what I really want to do is see the row clicked updated in my detailed view on the right. I am able to see in the Log window that I clicked on a certain index so I know I am capturing the click event and getting a value.
Inside my DetailedVC.m I have the following code to update this label.
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void) configureView {
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
NSLog(#"Item: %#", [self.detailItem description]);
}
}
If I edit the viewDidLoad I get a (null) for the [self.detailItem description]
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Detail description label: %#", [self.detailItem description]);
}
ProductViewController.h
#interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *_products;
UITableView *_productsTableView;
id<ProductSelectionDelegate> _delegate;
}
#property (nonatomic, strong) IBOutlet UITableView *productsTableView;
#property (nonatomic, retain) NSMutableArray *products;
#property (nonatomic, assign) id<ProductSelectionDelegate> delegate;
#end
AppDelegate.h
#class ProductViewController;
#class DetailedVC;
#interface TabAndSplitAppAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
ProductViewController *_productViewController;
DetailedVC *_detailedViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet ProductViewController *productViewController;
#property (nonatomic, retain) IBOutlet DetailedVC *detailedViewController;
#end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableArray *products = [NSMutableArray array];
[products addObject:[[Product alloc] initWithName:#"Product 1 Name" desc:#"Product 1 Description"]];
_detailedViewController.product = [products objectAtIndex:0];
// Override point for customization after app launch.
//create split view controller
RootVC *rvc=[[RootVC alloc] init];
rvc.title=#"Root VC";
DetailedVC *dvc=[[DetailedVC alloc] init];
dvc.title=#"Detailed VC";
_productViewController.delegate = _detailedViewController;
MySplitViewController *msc = [[MySplitViewController alloc] initwithLeftVC:rvc rightVC:dvc];
msc.title=#"First";
//create a temporary VC to show in second tab
SecondViewController *vc2 = [[SecondViewController alloc] init];
vc2.title=#"Second";
//make an array containing these two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:msc,vc2,nil];
[tabBarController setViewControllers:viewControllers];
tabBarController.view.backgroundColor=[UIColor blackColor];
for(UITabBarItem*t in tabBarController.tabBar.items)
{
t.image=[UIImage imageNamed:#"icon.png"];
t.badgeValue=[NSString stringWithFormat:#"%d",([tabBarController.tabBar.items indexOfObject:t]+1)];
}
//the views are retained their new owners, so we can release
[rvc release];
[dvc release];
[msc release];
[vc2 release];
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
MySplitViewController.h
#import <UIKit/UIKit.h>
#interface MySplitViewController : UIViewController
{
UINavigationController *leftController;
UINavigationController *rightController;
}
#property (nonatomic, retain) UINavigationController *leftController;
#property (nonatomic, retain) UINavigationController *rightController;
- (void)layoutViews:(UIInterfaceOrientation)orientation initialVerticalOffset:(float)offset;
- (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc;
#end
MySplitViewController.m
- (MySplitViewController*) initwithLeftVC:(UIViewController*)leftvc rightVC:(UIViewController*)rightvc
{
if(self=[super init])
{
UINavigationController *lnc=[[UINavigationController alloc] initWithRootViewController:leftvc];
lnc.navigationBarHidden=NO;
self.leftController=lnc;
[lnc release];
UINavigationController *rnc=[[UINavigationController alloc] initWithRootViewController:rightvc];
rnc.navigationBarHidden=NO;
self.rightController=rnc;
[rnc release];
}
return self;
}
Product.h
#import <Foundation/Foundation.h>
#interface Product : NSObject {
NSString *_productID;
NSString *_productDescription;
}
#property (nonatomic, copy) NSString *productID;
#property (nonatomic, copy) NSString *productDescription;
- (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription;
#end
Product.m
#import "Product.h"
#implementation Product
#synthesize productID = _productID;
#synthesize productDescription = _productDescription;
- (Product *)initWithName:(NSString *)productID desc:(NSString *)productDescription {
if ((self = [super init])) {
self.productID = productID;
self.productDescription = productDescription;
}
return self;
}
#end
Ok, please try this in ProductViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"Row clicked: %i", indexPath.row);
Product *product = (Product *) [_products objectAtIndex:indexPath.row];
NSLog(#"Product: %#", product);
TabAndSplitAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(#"appDelegate: %#", appDelegate);
UITabBarController *tbc = appDelegate.tabBarController;
NSLog(#"tbc: %#", tbc);
MySplitViewController *msvc = tbc.selectedViewController;
NSLog(#"msvc: %#", msvc);
UINavigationController *rnc = msvc.rightController;
NSLog(#"rnc: %#", rnc);
DetailedVC *dvc = rnc.topViewController;
NSLog(#"dvc: %#", dvc);
dvc.detailDescriptionLabel.text = #"We found our DetailedVC";
[dvc productSelectionChanged:product];
}
This code is not an example of how you should structure your program properly. This was just to help me (and you) understand your view controller hierarchy.
The initial idea of notifying a delegate when a product is selected is spot on, it just didn't work in your case because the objects weren't wired up properly. You should try to do that though. In the code you posted, I don't see a location where both the ProductViewControllerand the DetailedVC are both directly visible so that you could just say
productViewControllerInstance.delegate = detailedVCinstance;
The nearest to that is in AppDelegate where you have the RootVC instance rvcthat presumably eventually will create the ProductViewController and the dvc. Maybe you could give the dvcto the rvcso that it can set it as the ProductViewController's delegate when it's created. Good luck!

How to pass a value from one UIViewController to another

I am making an app where I have to pass a value from a second class to a first class. I have created a delegate method for that in second class.
In second class I have a UITextField, and if enter any text in this textfield it should be passed to a cell in a UITableView in first view.
However, in my case the value is not being passed properly. What have I done wrong?
This is my code:
second.h
#import <UIKit/UIKit.h>
#protocol secondDelegate<NSObject>
#required
- (void)setsecond:(NSString *)inputString;
#end
#interface second : UIViewController {
IBOutlet UITextField *secondtextfield;
id<secondDelegate>stringdelegate;
NSString *favoriteColorString;
}
#property (nonatomic, retain) UITextField *secondtextfield;
#property (nonatomic, assign) id<secondDelegate>stringdelegate;
#property (nonatomic, copy) NSString *favoriteColorString;
#end
second.m
#import "second.h"
#implementation second
#synthesize stringdelegate, secondtextfield, favoriteColorString;
- (void)viewWillDisappear:(BOOL)animated {
[[self stringdelegate] setsecond:secondtextfield.text];
favoriteColorString=secondtextfield.text;
NSLog(#"thuis check:%#", favoriteColorString);
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
//[[self stringdelegate] setsecond:secondtextfield.text];
//favoriteColorString = secondtextfield.text;
//NSLog(#"thuis check:%#", favoriteColorString);
}
#end
first.h
#import <UIKit/UIKit.h>
#import "second.h"
#import "TextviewExampleAppDelegate.h"
#interface first : UITableViewController<secondDelegate> {
//TextviewExampleAppDelegate *app;
TextviewExampleAppDelegate *check;
}
first.m
#implementation first
- (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] autorelease];
}
// Configure the cell...
cell.textLabel.text = #"message";
cell.detailTextLabel.text = check.favoriteColorString;
NSLog(#"this second check:%#", check.favoriteColorString);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
second *viewtwo = [[second alloc] initWithNibName:#"second" bundle:nil];
//viewtwo.favoriteColorString = indexPath;
viewtwo.stringdelegate = self;
[self.navigationController pushViewController:viewtwo animated:YES];
[viewtwo release];
}
- (void)setsecond:(NSString *)inputString {
if (nil != self.stringdelegate) {
[self.stringdelegate setsecond:inputString];
}
[self.tableView reloadData];
}
#end
remove delegate methods.
import your second class to first one.
in 2nd class import first class and implement id firstClass variable there.
when you pushing 2nd class, set id from (3) to self.
when you'v done and ready to pass it, set firstClass.passedValue = passingValue
pop second class
for example:
//first.h:
#import "second.h"
#class second
//second.h:
#import "first.h"
#class first
...
id firstClass;
//first.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
second *viewtwo =[[second alloc]initWithNibName:#"second" bundle:nil];
[self.navigationController pushViewController:viewtwo animated:YES];
viewtwo.firstClass = self;
[viewtwo release];
}
//second.m:
firstClass.passedValue = self.passingValue;
Please refer following rough scratch:
in application delegate .h
Create variable
NSString *varStr;
Assign Property
#propery (nonatomic, retain) NSString *valStr;
In delegate .m
#synthesize varStr;
initialize var
varStr = [NSString strinWithFormat:#"Hi"];
in First class
create delegate var;
delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate];
set value
var.varStr = [NSString strinWithFormat:#"First"];
get value
NSLog (#"%#",var.varStr);
in Second class
create delegate var;
delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate];
set value
var.varStr = [NSString strinWithFormat:#"Second"];
get value
NSLog (#"%#",var.varStr);

Resources