My problem is how to reload the tableview
I have 2 viewcontrollers.
In first Viewcontroller I have one tableview. if I select any row in tableview it goes to second viewcontroller.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NextPageController *nextView = [[NextPageController alloc] initWithNibName:#"NextPageView" bundle:nil];
[self.navigationController presentModalViewController:nextView animated:YES];
[nextView release];
}
in second view controller I have one textfield. If I enter any value into the textfield I need to disaplay that value into the first viewcontroller tableview.
can any one help me?
Thanks in advance.
give NextPageController a protocol and a delegate,just like this:
#protocol (NextPageControllerDelegate)
-(void)displayString:(NSString *)inputString;
#end
#interface FirstTableViewController : UITableViewController {
id<NextPageControllerDelegate> stringDelegate;
}
#property (nonatomic, assign) id<NextPageControllerDelegate> stringDelegate;
and in the .m file:
#implementation
#synthesize stringDelegate;
then, when you alloc the NextPageViewController, insert this:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NextPageController *nextView = [[NextPageController alloc] initWithNibName:#"NextPageView" bundle:nil];
nextView.stringDelegate = self;
[self.navigationController presentModalViewController:nextView animated:YES];
[nextView release];
}
of course, your table view controller must conforms to the NextPageControllerDelegate protocol, and give the implementation of
-(void)displayString:(NSString *)inputString;
after that, when you are in nextView, and want to have the table view display that string, you may do this:
if(nil != self.stringDelegate)
[self.stringDelegate displayString:someString];
ant then it's done
Make Global String ,
Assign it in secondViewController and when you pop from secondViewController to FirstViewController , viewWillAppear will call.
make logic such it uses global variable and then reload table ..
you need to have a global NSString variable in appDelegate that stores your text of text filed in second view.
Now, when you come back to previous page, in
- (void) viewWillAppear
{
[yourDataSourceArray addObject:appDelegate.yourGlobalString];
[yourTableView reloadData];
}
and yes make sure that your yourDataSourceArray is NSMutableArray.
Hope it helps you.
You could create a property for an NSString in your first view, and a property for an instance of the first view controller (UITableViewController?) in your second view. Then upon saving or popping or whatever you're doing in the second view controller once you have entered the text you want, you could set the property of the NSString, pop the view, and reload the tableView in viewWillAppear. Alternatively you could use delegation as Lewen described.
create global NSMutableArray.
Store and add all data in that NSMutableArray.
In -(void) viewwillappear of first class, call [tablename reloadData];
Related
I need help. I have a view with a tableView, when I click a cell, it shows me the other view with a mapView. How do you use a string with one class in another class?
Please write in map view controller
in .h file of Map view controller
#property(nonatomic,retain)NSString* strToSend;
in .m file of Map view controller
#synthesis strToSend;
in base viewController where TableView is programmed
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MapView* mapObj = [[MapView alloc]initWithNibName:#"MapView" bundle:nil];
mapObj.strToSend = #"Text To Send"; // you can send string to MapView controller via this string object
[self.navigationController pushViewController:mapObj animated:YES];
[mapObj release];
}
Create a #Property (nonatomaic,retain) NSString *stringToshow; in next view and assign its value in didSelectRowAtIndexPath delegate of UITableView.
You have 2 way yo do this task
1. By using getter and setter property(making #property()).
2. Using NSUserDefault
Just follow my answer to get Better result
passing the value from one class to other class
i am kind of making app which play audio using table view
After selecting particular row,it play audio on next view,but it does not solve my problem
Since want it to stop after dismissing the view and return to table view
So i want a way to play song on next view in such a way it stops after dismissing the view
or,
if i can access to the particular row(indexpathy.row) on next view
just property synthesize the raw no or int value to the nextview like bellow..
#interface NextViewController : UIViewController{
int rowNo;
}
#property (nonatomic, assign) int rowNo;
and synthesize in .m file like bellow...
#synthesize rowNo;
and when you push to nextview at that time set this property like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NextViewController *objNextViewController = [[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
objNextViewController.rowNo = indexPath.row;
[self.navigationController pushViewController:objNextViewController animated:YES];
[NextViewController release];
}
Simply take a variable in the next view's class and pass in the value of indexpath.row to it when you are selecting the cell. Since this variable has the value of row selected, you can access this value in next view.
I am developing an app which is similar to the settings app in iphone.
In my first VC i have a 4 rows upon selection it takes to the second VC. my second VC displays list of items. Once the user selected, the selected text should be displayed on the firstVC adjacent to the one selected.
How to achieve it with the help of dictionary objects or in other way.!Thanks in advance...
I did a similar task a few days back and here is how I accomplished that. Note, there might be many alternatives to achieving what you want.
KeyPoint: UITableView is dependent upon its DataSource (array or dictionary) for its data to be displayed in cell as well as number of sections and rows per section.
Now, initially your DataSource has values that are displayed as default. Upon tapping the row, initialize and push the 2ndViewController on navigation stack. In this 2ndViewController, you must somehow update the 1stViewController's DataSource (replacing original values).
Approach 1
You can use Protocols & Delegates
Create a protocol as following.
#protocol MyTableDelegate
- (void) dismissWithData:(NSString*)data;
Create a delegate reference in 2ndViewController, Call that delegate method and pass the selected data
#interface 2ndViewController : UIViewController
#property (assign) id<MyTableDelegate> delegate;
#property (assign) NSIndexPath *ip;
// Also synthesize it
#implementation 2ndViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.delegate dismissWithData:#"Second Table Selection"];
[self.navigationController popViewControllerAnimated:YES];
}
Now in 1stViewController, implement the TableView & Protocol Method
#interface 1stViewController : UIViewController <MyTableDelegate>
#implementation 1stViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
2ndViewController *controller = [[2ndViewController alloc] initWithNib....];
// Set the Delegate to Self
controller.delegate = self;
self.ip = indexPath;
// Push Controller on to Navigation Stack
}
- (void) dismissWithData: (NSString*) data
{
// We Will Store NSIndexPath ip in didSelectRowAtIndex method
// Use the ip to get the Appropriate index of DataSource Array
// And replace it with incoming data // Reload TableView
[self.dataSource replaceObjectAtIndex:[ip row] withObject:data];
[self.tableView reloadData];
}
Approach 2
You could also pass the data source to 2ndViewController in the didSelectRowAtIndexPath method..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
2ndViewController *controller = [[2ndViewController alloc] initWithNib....];
controller.dataArray = self.dataSource;
// Push Controller on to Stack
}
Then in the same didSelectRowAtIndexPath method of 2ndViewController, update the data source, and call
[self.navigationController popViewControllerAnimated:YES];
also, you need to reload your tableview in 1stController's ViewWillAppear method
- (void)ViewWillAppear:(BOOL)animated
{
[self.tableView reloadData];
[super ViewWillAppear:animated];
}
I think you need to call [self.tableView reloadData] in viewDidAppear. If possible do go through this link Reload UITableView when navigating back?
The array that you are using in your first VC. Pass that array to the second VC, update it there in the secondVC class and write this statement "[firstVC.tableView reloadData]" in the viewWillAppear method of your firstVC class.
This will update your array of firstVC class in secondVC class when user selects anything in secondVC class, and than when user naivgates back table view of firstVC is reloaded. So that the update array is reflected in table view.
So I thought I'd have a go at building my own simple app. Please go easy on me I'm new to all this! The idea is this. For iPad have a single view controller with a text box and a text field. Text box takes a title, and text field takes the body of a report. There's a button on the page to submit the report, which bundles the two texts into an object and adds it to a table view within the same view controller. I have set the view controller as a delegate with <UITableViewDelegate, UITableViewDataSource> in my header file. My table view works fine for adding items in the viewDidLoad method. But adding items from the text inputs via a UIButton connected to -(IBAction) addItem falls over with: Property 'tableView' not found on object of type 'ReportsViewController'
- (IBAction)addReportItem
{
int newRowIndex = [reports count];
ReportObject *item = [[ReportObject alloc] init];
item.title = #"A new title";
item.reportText = #"A new text";
[reports addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
I understand that I'm trying to call a method within my object but I have other method calls to tableView which work fine. i.e.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [reports count];
}
I thought this was the point of delegation. I know I'm missing something, but as I say I am new to all this and have looked everywhere for an answer before posting. What do I need to do to send my IBAction message to tableView?
Do you have a tableView instance variable setup in your .h file of the view controller?
The reason you are able to access it in the delegate and data source methods is because they are passed in as part if the methods.
You will need to add the IBOUTLET tableView ivar and connect it to the tableView in your .xib.
Or perhaps your ivar for the tableView is named something else?
Good luck.
I had the same problem.
What helped was to inherit the View Controller from UITableViewController, instead of UIViewController. Not using the protocol names in angled brackets.
The TableView is then linked to the dataSource and delegate via the storyboard (resp. InterfaceBuilder).
The parent class UITableViewController has an IBOutlet tableView defined.
MyViewController.h:
#interface MyViewController : UITableViewController
I have a UIViewController (AbcViewController) with NavigationController. AbcViewController uses UIView (AbcView) as its view. AbcView has a UITableView. I have set this TableView's datasource in AbcViewController and delegate in its SuperView i.e. AbcView. How will I push another UIViewController (XyzViewcontroller) into navigationController when I select a row in this table because it gives
"error: request for member 'navigationController' in something not a structure or union"
when I do this:
[self.navigationController pushViewController:xyzViewcontroller animated:TRUE];
I also did this:
AbcViewController *parentVC;
[parentVC.navigationController pushViewController:xyzViewcontroller animated:TRUE];
Though it builds successfully, XyzViewcontroller's view does not appear. It does not push XyzViewcontroller's view onto navigationController.
We can make a delegate on AbcView so that we can refer back to the view's viewController which in this case is AbcViewController. We can set delegate as:
#interface AbcView : UIView
{
id delegate;
}
#property(nonatomic, assign) id delegate;
Then in AbcViewController, set the view's delegate like this:
[abcView setDelegate:self];
Now in table method using our delegate from within AbcView as:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[delegate pushOtherView];
}
it will call - (void)pushOtherView method in AbcViewController whose definition is:
- (void)pushOtherView
{
XyzViewcontroller * xyzViewcontroller = [[[XyzViewcontroller alloc] init] autorelease];
[self.navigationController pushViewController:xyzViewcontroller animated:YES];
}
But before using this method, it should be mentioned in protocols otherwise it will show
Warniing:
-pushOtherView' not found in protocol(s).