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];
Related
sending text to label and number to int from tabelview to viewcontroller but it is not working
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
pushnavViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"pushnavViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.pushh.text = [IDNum objectAtIndex:indexPath.row];
detailViewController.pushh.text = [listt objectAtIndex:indexPath.row];
}
I also tried to do that , but not working :
detailViewController.pushh.text = [IDNum objectAtIndex:indexPath.row];
detailViewController.pushh.text = [listt objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
i made all the property and synthesize them :
#property (nonatomic) UILabel *pushh;
#property (nonatomic) UILabel *IDnumber;
so, any idea?
NSArray *listt,*IDNum;
Well, for one you are using detailViewController.pushh.text for both assignments. Should be detailViewController.IDnumber.text for the first one, I believe. Secondly, you can't change the .text property of the label until after the view is loaded.
Instead, create an NSString property and save your information to the NSString, then in your viewDidLoad method of you pushnavViewController assign the label.text with the value of the NSString property.
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.
Hi I tried another code for passing data but it is not working. How to pass UITextField value on firstview on UITableViewCell on row? Please someone help me on delegate protocol
//
// first.h
// TextviewExample
//
// Created by pradeep.yadav on 12/5/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "second.h"
#import "TextviewExampleAppDelegate.h"
//#class second;
#interface first : UITableViewController <secondDelegate>{
//TextviewExampleAppDelegate*app;
//TextviewExampleAppDelegate *check;
second *secondview;
NSString *secondFavoriteColorString;
//second *value;
}
#property (nonatomic, retain) second *secondview;
#property (copy) NSString *secondFavoriteColorString;
#end
- (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=secondFavoriteColorString;
NSLog(#"this second check:%#",secondFavoriteColorString);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
second *viewTwo = [[second alloc] initWithNibName:#"second" bundle:[NSBundle mainBundle]];
self.secondview = viewTwo;
[viewTwo release];
[self.navigationController pushViewController:self.secondview animated:YES];
}
-(void)setsecond:(NSString*)secondtextview
{
secondFavoriteColorString = secondtextview;
NSLog(#"this second check:%#",secondFavoriteColorString);
}
this second class
//
// second.h
// TextviewExample
//
// Created by pradeep.yadav on 12/5/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#protocol secondDelegate<NSObject>
#required
-(void)setsecond:(NSString*)secondtextview ;//forIndexPath:(NSIndexPath*)indexPath;
#end
#interface second : UIViewController {
IBOutlet UITextField *secondtextfield;
NSString *favoriteColorString;
id <secondDelegate> delegate;
}
#property (nonatomic,retain)UITextField *secondtextfield;
//#property (nonatomic,assign)id<secondDelegate>delegate;
#property (retain) id delegate;
#property (nonatomic,copy)NSString *favoriteColorString;
#end
#import "second.h"
#implementation second
#synthesize delegate,secondtextfield,favoriteColorString;
- (void) viewWillDisappear:(BOOL) animated
{
[[self delegate] setsecond:secondtextfield.text];
favoriteColorString=secondtextfield.text;
NSLog(#"thuis check:%#",favoriteColorString);
}
- (BOOL) textFieldShouldReturn: (UITextField *) theTextField
{
[theTextField resignFirstResponder];
return YES;
}
I think the trouble is that you have declared the delegate in the second view, and you are trying to call it from the first view. By the time you have returned to the first view, it's very likely that the second view has been cleaned up.
What you want to do is declare the delegate methods in the first view controller, and when you create the second view controller, you set the first view controller to be the delegate of the second view controller.
I created an example project a while ago. It doesn't deal with a table view, but it demonstrates passing a string back from a second view controller to the first view controller.
Here is a link to a tutorial how to share data between viewController:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-other-objects.html
It should help you out to solve your problem.
From TextField in View1 to Cell in View2
Take a String in Second View.
Property, synthesize it,
While moving from first view to second view Assign the value of UITextField to the synthesized string right after allocating and before pushing.
In second view display the text in synthesized string on the cell.
From TextField in View2 to Cell in View1
Take a String in First View.
Property, synthesize it,
Take a object of FirstView in secondView and property and synthesize it
While moving from first view to second view Assign the value of objSecondView.objFirstView=self; after allocating and before pushing.
Now in second view assign the value of text to firstViews synthesized string.
And in viewWillAppear of firstView assign the value of its string to the cell
E.g. Your method in the above example must look loke the one below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
second *viewTwo = [[second alloc] initWithNibName:#"second" bundle:[NSBundle mainBundle]];
viewTwo.objViewOne = self;
[self.navigationController pushViewController:viewTwo animated:YES];
[viewTwo release];
}
Thats it.. Comment if still any problem faced.
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
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.