detailview controller value not showing - iphone

While using the DetailViewController the value is not passing from the UITabelViewController, is it any wrong with the code?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detailedvu *dvController = [[detailedvu alloc] initWithNibName:#"detailedvu" bundle:[NSBundle mainBundle]];
if (0 == indexPath.row)
{
NSLog(#"0");
dvController.lb1.text = #"Make";
}
[self.navigationController pushViewController:dvController animated:YES];
}
Please help me to sort out.

The dvcontroller is instantiated correctly.The problem here is that the lbl is a label from the nib(i think).So when pushed to the viewcontroller a new instance is created so the setting here does not make any sense.
Pass it in a data holder such as NSString and load the label in the viewdidLoad of the detailedvu
One suggestion : Please try to follow the naming conventions for better readability
Happy Coding :)

You can achive this using this.
Create a string variable property in detailedvu.h file :
#property (strong, nonatomic) NSString * str;
And in detailedvu.m file add this :
#synthesize str;
- (void)viewDidLoad
{
[super viewDidLoad];
if (str.length!=0) {
NSLog(#"hi%#",str);
lb1.text=str;
}
}
And in your table vc .m file put this code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detailedvu *dvController = [[detailedvu alloc] initWithNibName:#"detailedvu" bundle:[NSBundle mainBundle]];
if (0 == indexPath.row)
{
NSLog(#"0");
dvController.str = #"Make";
}
[self.navigationController pushViewController:dvController animated:YES];
}

Related

How to push view depending on the row selection?

/*****UPDATED** ***/r.com/YH3cm.png
I am trying to figure out in the above image, how will we know if the user has selected Date or Track.
/UPDATED/
The data I am receving is through a select query and I create an array to store the list. It is dynamic and not necessary limited to two fields, it can have 10 fields also. How will I know which row is selected and how will I push the data on to the next view.
Like in didSelectRowAtIndexPath, how should I push the date or track field on the next view?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (dvController == nil)
dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
Teat *obj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
dvController.obj = obj;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
}
It's still not very clear what you're trying to do. If you want to push a certain view controller depending on what the content of the cell is, but there is no definite arrangement of the rows, I would use the row index to access the array that is the source of your data. Some very loose code:
WhateverObject* selectedObject= (WhateverObject*)[tableDataSourceArray objectAtIndex:indexPath.row];
if( [selectedObject hasAnAttributeYouCareAbout] )
{
MyViewController* theCorrectController= whicheverViewControllerYouWant;
theCorrectController.anAttribute= aValue;
[self.navigationController pushViewController:theCorrectController animated:YES];
}
And here's how you can define your UIViewController subclass MyViewController with specific attributes. In the .h file:
#interface MyViewController : UIViewController {
int anAttribute;
}
#property int anAttribute
#end
In the .m file:
#implementation MyViewController
#synthesize anAttribute;
#end
You can have as many attributes as you want of whatever type, and then you can set them with aViewController.anAttribute as above.
Create objects - dateInfoViewController and trackInfoViewController and then...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
if (row==0)
{
if (self.dateInfoViewController == nil)
{
DateInfoViewController *temp = [[DateInfoViewController alloc] init];
self.dateInfoViewController = temp;
[temp release];
}
else {
dateInfoViewController.title= [ NSString stringWithFormat:#"%#", [sessionInfoDetailsArray objectAtIndex:row]];
YourAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[delegate.sessionNavigationController pushViewController:dateInfoViewController animated:YES];
}
}
if (row==1)
{
if (self.vetInfoViewController == nil)
{
TrackInfoViewController *temp = [[TrackInfoViewController alloc] init];
self.trackInfoViewController = temp;
[temp release];
}
else {
trackInfoViewController.title= [ NSString stringWithFormat:#"%#", [sessionInfoDetailsArray objectAtIndex:row]];
YourAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[delegate.sessionNavigationController pushViewController:trackInfoViewController animated:YES];
}
}
I fear it's not perfectly clear what do you want to do... if you need to push a different view depending on the selected row you may simply do something like
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
//push view 1
else
//push view 2
}
UPDATE: calling indexPath.row you get the index of the selected row. I guess is up to you to decide what to do depending on what row is selected. To pass this information to the next view you may simply think of a #property field to set, a method to call or a custom init method for the view controller you are pushing. What is the problem with the code you posted?

Loading problem of NIB file from TabViewController in iPhone

I have a UITableViewController (MyViewController.xib). This is showing 3 rows with their title. I have 3 new xib file for each row title.On each row selection I want to load XIB file. I am getting the place when I am clicking on RowIndex Selection. But when i am trying to load NIB file nothing is happening. I mean nither program is being crashed nor NIB file is being load.
I am defining my interface declaration here.
#import <UIKit/UIKit.h>
#import "HistoryShow.h"
#interface MyViewController : UITableViewController {
NSArray *tableList;
IBOutlet HistoryShow *historyController;
}
#property (nonatomic,retain) NSArray *tableList;
#property (nonatomic,retain) IBOutlet HistoryShow *historyController;
#end
My implementation details are below.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"%#",[tableList objectAtIndex:indexPath.row]]; //typecasting
if([#"History" isEqual:str])
{
NSLog(#"!!!!!!!!!!!!!!!");
HistoryShow *detailViewController = [[[HistoryShow alloc]initWithNibName:#"HistoryShow" bundle:nil]autorelease];
[historyController release];
}
}
This is prining "!!!!!!" on console but next "HistoryShow.xib" is not being load.
What is the exact problem ?
Thanks in advance.
You have to add the view to your present view using addSubview: or push the viewController using a navigationController to see the view.
Something like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"%#",[tableList objectAtIndex:indexPath.row]]; //typecasting
if([#"History" isEqual:str])
{
NSLog(#"!!!!!!!!!!!!!!!");
HistoryShow *detailViewController = [[HistoryShow alloc]initWithNibName:#"HistoryShow" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES]; // if you have a navigation controller
[detailViewController release];
}
}
You are instantiating detailViewController, but you aren't doing anything with it.
Try adding this after the alloc of detailViewController:
[self presentModalViewController:detailViewController animated:YES];

Load specific URL after pushing from UITableView

I create UITableView and I have 3 cells , so each cell should push to DetailViewController to load specific URL . On detailViewController I have UIWebView that load url after user select a cell of tableView , But I don't know how can I do this things ! . Here is my code
.h:
#import "webController.h"
#class webController;
#interface RootViewController : UITableViewController <UITableViewDelegate , UITableViewDataSource> {
NSArray *list;
webController *webcontroller;
}
#property (nonatomic , retain) IBOutlet NSArray *list;
#end
.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!webcontroller) {
webcontroller = [[webController alloc] initWithNibName:nil bundle:nil];
webcontroller.navigationItem.title = [list objectAtIndex:indexPath.row];
[self.navigationController pushViewController:webcontroller animated:YES];
}else {
webcontroller.navigationItem.title=[list objectAtIndex:indexPath.row];
[self.navigationController pushViewController:webcontroller animated:YES];
}
}
I would like to what iHS said:
The best way (in my opinion) would be to have two NSArray's. One to display the name (google, apple, yahoo) and then one to store the specific URL.
So you could do something like this
in .h:
NSArray *urlArray;
and then in the .m:
(modified iHS's code)
if (!webcontroller) {
webcontroller = [[webController alloc] initWithNibName:nil bundle:nil];
}
webcontroller.navigationItem.title=[list objectAtIndex:indexPath.row];
webcontroller.strUrl = [urlArray objectAtIndex:indexPath.row]; //Load the url at the specific index in "urlArray"
[self.navigationController pushViewController:webcontroller animated:YES];
Post any questions below ;)
You can achieve this by various methods. The simpler and best would be to create a property in your webController class
#property(nonatomic, retain) NSString *strUrl
and then synthesize it by using
#synthesize strUrl in your webController.m
Now in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!webcontroller) {
webcontroller = [[webController alloc] initWithNibName:nil bundle:nil];
}
webcontroller.navigationItem.title=[list objectAtIndex:indexPath.row];
webcontroller.strUrl = [list objectAtIndex:indexPath.row];
[self.navigationController pushViewController:webcontroller animated:YES];
}
Then use this strUrl to load the page in webview. I hope it helps

Not Able to set title and Navigate to the next view by clicking on corresponding row in uitableview

i tried to understand the uitable view and implemented it and able to populate data into row from a nsArray.I have make a NextViewController consisting of a dynamic lable which will receive inpur/text from the SimpleTable class.For ex,
in simple table view class i have rows with content as(see code please):
**My SimpleViewController.m**
#import "SimpleTableViewController.h"
#import "NextViewController.h"
#implementation SimpleTableViewController
- (void)viewDidLoad {
arryData = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
self.title = #"Simple Table Exmaple";/**/NOT ABLE TO SET TITLE.WHY?????Please guide how to use NSlog here**
[super viewDidLoad];
}
***//And on selection of any row i want to navigate to next view controller class***
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NextViewController *nextController = [[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
[nextController changeProductText:[arryData objectAtIndex:indexPath.row]];
}
#end
**My NextViewController.h**
#interface NextViewController : UIViewController {
IBOutlet UILabel *lblProductTxt;
}
- (IBAction) changeProductText:(NSString *)str;
#end
**my NextViewController.m**
//posted only important part of code where i feel may be an issue
- (IBAction) changeProductText:(NSString *)str{
lblProductTxt.text = str;
} #end
I have check IB properly and the connections are proper.Please help me.Not getting where i am wrong?
Make sure self.navigationController has a valid value. Add this before the pushViewController call:
NSLog(#" self.navigationController 0x%x", self.navigationController);

Call UIViewController from "subview"

I have a UIViewController which when it loads it loads up this..
MapViewController *mapController = [[MapViewController alloc] initWithNibName:#"MapView" bundle:nil];
self.mapViewController = mapController;
[self.view insertSubview:mapController.view atIndex:0];
[mapController release];
I also have a switch views button that can change to a table view....
if (self.tableViewController ==nil)
{
TableViewController *tableController = [[TableViewController alloc] initWithNibName:#"TableView" bundle:nil];
self.tableViewController = tableController;
[tableController release];
//[self.view insertSubview:detailController atIndex:0];
}
if (self.mapViewController.view.superview == nil)
{
[tableViewController.view removeFromSuperview];
[self.view insertSubview:mapViewController.view atIndex:0];
}
else
{
[mapViewController.view removeFromSuperview];
[self.view insertSubview:tableViewController.view atIndex:0];
}
I am trying to change the view to a detail view based on selecting a row in the table view and I cannot work out how to call it at all. All methods I have seem to fail! Please help!
Add the UITableViewDelegate protocol to your controller class.
#interface myTableViewController : UITableViewController <UITableViewDelegate>
When you create your table veiw controller set its delegate to be your controller using:
myTableViewController.delegate = self; // Assuming your setup code runs within the table view controller
In your tableViewController, implement didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowSelected = [indexPath indexAtPosition:0]; // Assuming your UITableView has a single section.
The class you should look at for handling row selection is UITableViewDelegate, which has the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath