Reload data of table view subobjec of uiviewcontroller with tableview - iphone

i've a class SelezionaProfilo, in this class i've a list of players:
#interface SelezionaProfilo : UIViewController < UITableViewDelegate, UITableViewDataSource > {
UITableView *myTableView;
//my staff
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
all works fine.
i've built a subclass SelectPlayerViewController embeded in navigationcontroller, because i want to have a different reaction to push a row:
#interface SelectPlayerViewController : SelezionaProfilo {
all works fine, i've the players list. And i have the different action.
in the SelectPlayerViewController.m i've added a button on navigation bar, add player button that goes to another class in push navigation.
Now the player was created, but when the view come back to SelectPlayerViewController, i can't see the last player.
in SelezionaProfilo.m:
- (void)viewWillAppear:(BOOL)animated {
[self.myTableView reloadData];
[super viewWillAppear:animated];
}
and in SelectPlayerViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super.myTableView reloadData];
}
i'm sure that enter in this method, but the table doesn't refresh.
I've a tabbar, and i've SelezionaProfilo in another tab, if i select this, i can see the last player, so the SelezionaProfilo class works fine.
can anyone suggets me?
thaks

I resolved it!!!
so if interest to someone....
i've a table view in the xib, i must connect it with File's Owner two Referencing Outlet:
view
myTableView

Related

Xcode/Obj-c - Segue pushes new instance

I have seen similar questions a lot on Stackoverflow and I tried a lot of things but I can't seem to figure this out. I have multiple TableViewControllers and 1 MainViewController. The MainViewController has buttons calling the different TableViewControllers and on selecting a tablecell the tableViewController dismisses.
The problem is that im pushing a new instance of my MainViewController every time I push from either one of my tableViewControllers. I currently use Segues to push between these different controllers.
In short: When switching from TableViewControllers to ViewController I want to prevent the ViewController to get pushed as a new instance because this way its removing my previous data input.
Im pretty sure I have to use either:
[self dismissModalViewController: withCompletion:]
performSegue
prepareForSegue
Or set some global variables in a class and call those, but im not experienced enough yet to implement this correctly.
A simple example of end result would be: 3 textfields in VC. On clicking textfield1 it opens tableview1 and on clicking a cell it updates textfield1. Textfield2 opens tableview2, etc.
Hope im clear enough, could post sample code if needed.
Edit, posting code (keep in mind, segues are performed in storyboard):
TableViewExample.h:
#interface IndustryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *tableViewArray;}
#property (nonatomic, strong) IBOutlet UITableView *tableViewIndustry;
TableViewExample.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showIndustry"]) {
NSIndexPath *indexPath = [self.tableViewIndustry indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.industryText = [tableViewArray objectAtIndex:indexPath.row];
destViewController.industryTextName = [tableViewArray objectAtIndex:indexPath.row];
}}
Then in ViewController.m, viewDidLoad:
[industry setTitle:industryText forState:UIControlStateNormal];
These are the most important parts I think.
Is the segue of type "Push"? If so you should try dismissing the table view controllers using:
[self.navigationController popViewControllerAnimated:YES];
If the segue is of type "Modal" instead you should do something like this on your table view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// your logic here
[self dismissModalViewControllerAnimated:YES];
}
As for the data exchange between controllers what I would personally do is creating a public property in the header file of the Table View Controller, like the following:
#property (nonatomic, weak) <Your_UIViewController_Subclass_Here> *mainController
Than, in the main controller, override the prepareForSegue:sender: method to set the newly created property to point to the main controller, like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
<Your_Subclass_Of_UITableViewController_Here> *destinationController = segue.destinationController;
destinationController.mainController = self;
}
Now the Table View Controller will have a pointer to the main controller to send the data basically all you have to do is to implement some public method or property in the Main Controller to be called when the user selects a table view row in the table view controller in order to update the text in the textfields or whatever data model you are using.

Assign own View Classes to Storyboard

See this storyboard containing a Navigation View, Gestures, and a few Views.
Also within is a TabView, which contains an UIImageView and UITableViewController.
To apply my data to this TableView, I need to assign a Controller by using msExampleTableViewController.
The problem: the input/dropdown field removes entry after pressing Enter (like if this class is not assignable).
What is wrong here, and how can it best be done?
NA UITableViewController does assume that the root view will be a UITableView. When you combines UITableView with others controllers.. you can do in your Controller (ViewController in your case):
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, assign) IBOutlet UITableView* tableView;
In Interface Builder set both the delegate and dataSource outlets of the table view to be equal to its superview's view controller (File's Owner)...
Now, Your view controller implementation should be the typical TableViewController implementation: cellForRowAtIndexPath, etc.

How to pass a value to parent view controller? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
dismissModalViewController AND pass data back
I'm new to ios development and stuck on this problem:
I'm using storyboarding and have a navigation controller, vcA, with a TableView in it which shows some data from a MutableArray (which is initialized in viewdidload of the same class). After selecting any cell, a second view controller, vcB, is shown with a TextField in it and a button called "Add to list".
What I want is that when I enter some text in the TextField and press the "Add to list" button the text should be added to the array of previous view (which gets shown in the TableView) and when i tap the "Back" button on vcB's navigation bar, vcA should show the updated TableView with the new entry in it (on the top of list). Basically I want to add the text from vcB's TextField to the array of vcA and show the new array after clicking the BACK button.
I have searched a lot about this issue and seem to find that delegate and protocols is the way to achieve the desired result but I'm having trouble understanding delegation.
I have the second view controller presenting itself as modal in this example:
In the second view controllers h file:
#protocol SecondViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSString *)item;
#end
#interface SecondPageViewController : UIViewController <UITextViewDelegate>
{
NSString *previouslyTypedInformation;
}
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *previouslyTypedInformation;
#property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;
In the second view controllers m file make sure to synthesize properties and add then add this:
- (IBAction)done:(id)sender
{
NSString *itemToPassBack = self.textView.text;
NSLog(#"returning: %#",itemToPassBack);
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
//dismiss modal view controller here
}
Then in the first view controllers h file set it as delegate:
#interface FirstPageViewController: UIViewController <SecondViewControllerDelegate>
#property (nonatomic) NSString *returnedItem;
Then in the first view controller's m file synthesize and add the method:
- (void)addItemViewController:(SecondPageViewController *)controller didFinishEnteringItem: (NSString *)item
{
//using delegate method, get data back from second page view controller and set it to property declared in here
NSLog(#"This was returned from secondPageViewController: %#",item);
self.returnedItem=item;
//add item to array here and call reload
}
Now you have the text of what was returned! You can add the string to your array in the first view controller's viewDidLoad and call
[self.tableView reloadData];
and it should work.

How to detect the rotation change when coming back to tableView from detailView?

I have a UITableViewController and DetailViewController that pops up when concrete row is selected. The problem is the tableView does not call "didRotateFromInterfaceOrientation" method when rotation is did in DetailViewController. It's logical, however I need to reload a table if orientation has changed.
I'm trying this code but it doesn't work:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.interfaceOrientation != [UIDevice currentDevice].orientation){
[self handleTableViewRotation];
}
}
I'm assuming there should be some property that holds orientation of tableView that is independent of current device orientation property...
Maybe the most elegant solution is coding a protocol. Whenever your DetailViewController enters in a rotation callback, call reloadData method of your tableView.
Or... you can continue that way. In that case you have to store the tableViewController's last orientation as a instance var and compare it to the current. After that, remember setting the new orientation
EDIT: First declare a new Protocol.
#protocol RotationDelegate
- (void) didRotate;
#end
In your DetailViewController.h:
#import RotationDelegate.h
#interface DetailViewController : UIViewController {
id <RotationDelegate> rotationDelegate;
}
#property (nonatomic, assign) id <RotationDelegate> rotationDelegate;
In DetailViewController.m:
#synthesize rotationDelegate;
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[rotationDelegate didRotate];
}
Now your ViewController that contains the tableView:
#interface RootViewController : UITableViewController <RotationDelegate> {
...
}
And finally implement didRotate method and call [yourTableView reloadData] inside. Remember that you have to set the rotation delegate after init your DetailViewController.

reload tableview from navigation controller

I am new to iPhone development.
I have a navigation controller which loads a tableview. The class identity of the tableview is a tableViewController class.
I want to know how to refresh the table view from the navigation controller.
I know within the tableViewController i can use the
[self.tableView reloadData];
Sorry if i am vague.
Thanks in advance.
after feedback:
Here is the navigation controller
#interface slNavController : UINavigationController{
UIToolbar *toolbar;
slTableViewController *tableVC;
}
#property(nonatomic, retain) slTableViewController *tableVC;
where slTableViewController is the Delegate of the tableView that needs to be refreshed.
and where i needed to refresh the table i used
[tableVC.tableView reloadData];
Here is slTableViewController header
#interface slTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *slTableView;
NSMutableArray *list;
TryAppDelegate *appDelegate;
//UIToolbar *toolbar;
IBOutlet UITextField *textField;
}
But still not able to refresh the tableview.
There must be some viewController which is the delegate of the tableView. Send reloadData to that controller, e.g.
[viewController.tableView reloadData];
If you know where in the stack the viewController is, you can access it through the navController. For instance, if it happens to be the rootViewController, then you can do
HomesViewController *homesController = [[[self navigationController] viewControllers] objectAtIndex:0];
If it is Navigation based application you want to reload the data of table view, You have to write in view will appear like this
[self.tableView reloadData];
Give your navigationController a pointer to the tableViewController.
In the .h file:
#property(nonatomic, retain) YourTableViewController *tableViewController;
Then when you want to reload data:
[tableViewController.tableView reloadData];