Load specific URL after pushing from UITableView - iphone

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

Related

detailview controller value not showing

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];
}

Pass a value to the text field of the first view

I have two views with navigation controller: first view, there are empty text fields while the second view is a table view where the user can select a row. At the touch of a row there is an action that sets a value to a text field of the first view. Unfortunately when I go back to the first view field is not set.
This is my code:
FirtViewController.h
#interface FirstViewController : UIViewController
{
UITextField *firstField;
UITextField *secondField;
}
#property(nonatomic, retain) IBOutlet UITextField *firstField;
#property(nonatomic, retain) IBOutlet UITextField *secondField;
#property(copy) NSString *selectedRow;
-(IBAction)showTable:(id)sender
FirstViewController.m
#import "FirstViewController.h"
#import "AppDelegate.h"
#import "SecondViewController.h"
#implementation FirstViewController
#synthesize .....
.............
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.firstField.text = selectedRow;
}
-(IBAction)showTable:(id)sender
{
SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controllerSecond animated:YES];
}
SecondViewController.h
#class FirstViewController;
#interface ListaViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
{
UITableView *table;
UISearchBar *search;
FirstViewController *controller;
}
#property (nonatomic, retain) FirstViewController *controller;
SeconViewController.m
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedRow = [tableData objectAtIndex:indexPath.row];
controller.selectedRow = selectedRow;
[self.navigationController popViewControllerAnimated:YES];
}
Based on your code above, you never set the connection in secondViewController back to first
You probably need something like this:
-(IBAction)showTable:(id)sender
{
SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[controllerSecond setController:self]; //this sets the reference back
[self.navigationController pushViewController:controllerSecond animated:YES];
}
What you need to use is delegate. It is very commonly used pattern in Object-C. Check out my answer to this SO post. Let me know if you still need code after.
I'm not certain I fully understand what you mean, but why don't you creating an NSString containing the value you need in the SecondViewController. That way you can do something like this when you set up the SecondViewController.
SecondViewController *nextView = [[SecondViewController alloc] init];
nextView.myNSString = #"Value you need in the next view";
Then in SecondViewController.h you'll be able to access the NSString like so:
#NSLog(#"%#", self.myNSString);
The main thing you are missing is the Value passing between views. You are not using any object in any of the view that is passing the value to another view. Like, If you want to pass a text from FirstViewController to SecondViewController. You Can do it as follwos.
NSString *textToPass = self.firstField.text;
In Your SecondViewController there need to be a string object say passedText. When you create object of secondViewController in firstViewController, Do it as follows:
SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
controllerSecond.passedText = textToPass;
[self.navigationController pushViewController:controllerSecond animated:YES];
Now Wherever you want to show the text of first screen, just use "passedtext" String.
Hope it Helps.
:)
===============================================
In your code you may try the followinf modification:
NSString *selectedRow = [tableData objectAtIndex:indexPath.row];
controller = (FirstViewController *)[self.navigationController topViewController];
controller.selectedRow = selectedRow;
[self.navigationController popViewControllerAnimated:YES];
It Will resolve your problem forsure.

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];

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);

Pushing UITableViewController onto [self navigationController] causes an EXC_BAD_ACCESS

In the current view that I am in, a button touch-up-inside event leads to the following action:
(Note that while in the Debugger, I've verified that both [self navigationController] and the instantiated historyViewController do indeed exist.
I am unable to determine why this bad access is happening. I can pop/push this view/other views from the navigation controller. Any ideas on how to go about investigating why this view in particular is having problems when getting pushed onto the nav controller?
-(IBAction) viewOrEditHistory: (id) sender {
HistoryViewController *historyViewController = [[HistoryViewController alloc] initWithStyle:UITableViewStyleGrouped];
historyViewController.title = #"View or Edit by date";
historyViewController.sameExSessions = [[NSMutableArray alloc] init];
historyViewController.exercise = [[Exercise alloc] initWithName:self.title muscleGroup:muscleGroupLabel.text];
/*** EXC_BAD_ACCESS happens after following line is executed ***/
[[self navigationController] pushViewController:historyViewController animated:YES];
}
Here is my HistoryViewController.h
#import
#interface HistoryViewController : UITableViewController {
NSMutableArray *sameExSessions;
Exercise *exercise;
}
#property (nonatomic, retain) NSMutableArray *sameExSessions;
#property (nonatomic, retain) Exercise *exercise;
-(NSMutableArray *) SameExerciseSessionList;
-(NSString *) getDocPath;
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection: (NSInteger)section;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath;
#end
Please also get your memory management straight or you will run into a lot more problems. Every alloc should be followed by either a release or an autorelease.