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];
Related
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.
My problem is that in ProductNote class UIButton Action I did initWithNibName Notes Class to show the popOver with a UITableView in Notes Class. I am fetching data from sqlite and then load it to UITableView in tableViewDidSelectRowAtindexPath:. I got the selectedNote and creates object of ProductNote Class to call selectedNote instance method that only set IBOutlet's textview.text but its nil. Thats my problem, Below is the code please help me to knowing why i face this type of issue. I am using Xcode 4.3.3 and not using ARC. Manually I defined dealloc method on every ViewController
//**ProductNote.h Class****************************
#import <UIKit/UIKit.h>
#class Notes;
#interface ProductNote : UIViewController<UIPopoverControllerDelegate>
{
UIPopoverController *popOverController;
UITextView *txtmesssagenotes;
Notes *objNotes;
}
#property (retain, nonatomic) IBOutlet UITextView *txtmesssagenotes; //It is Connected to ProductNote.xib
#property (retain,nonatomic) UIPopoverController *popOverController;
#property (retain,nonatomic) Notes *objNotes;
-(IBAction)presentPopOver:(UIButton*)sender;
-(void)selectedNote:(NSString*)note;
#end
//ProductNote.m Class
#implementation ProductNote
#synthesize txtmesssagenotes,popOverController,objNotes;
-(IBAction)presentPopOver:(UIButton*)sender
{
self.objNotes=[[Notes alloc]initWithNibName:#"Notes"bundle:nil];
UIPopoverController *popOver=[[[UIPopoverController alloc]initWithContentViewController:objNotes]autorelease];
self.popOverController=popOver;
self.popOverController.delegate=self;
self.popOverController.popoverContentSize=objNotes.view.frame.size;
CGRect rect=[self.view convertRect:objNotes.view.frame fromView:self.view];
rect.origin.y+=110;
rect.origin.x+=23;
[self.popOverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];
//Then I get popOver with tableview
}
-(void)selectedNote:(NSString*)note
{
self.txtmesssagenotes.text=note;
//Here I am getting txtmesssagenotes=0X0 or nil. Using self or without self Please tell me the reason.
}
- (void)dealloc {
[txtmesssagenotes release];
[popOverController release];
[objNotes release];
[super dealloc];
}
#end
#interface Notes : UITableViewController
{
NSMutableArray *arrNotes;
NSString *selectedNote;
UITableView *tblNotes;
}
#property(nonatomic,retain)NSMutableArray *arrNotes;
#property(nonatomic,retain)NSString *selectedNote;
#property(nonatomic,retain)IBOutlet UITableView *tblNotes;
#end
//Actually I skip the other methods that makes larger program to read . other methods only used to fetch data from sqlite and fill up the arrNotes that is used to fill all rows in the tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedNote=[self.arrNotes objectAtIndex:indexPath.row];
//Here self.arrNotes valid object and contains the data, and assigns NSString object to self.selectedNote
ProductNote *productNote=[[ProductNote alloc]init];
[productNote selectedNote:self.selectedNote]; //after calling selectedNote i goto to selectedNote implementation or definition above.
[productNote release];
}
-(void)dealloc
{
[arrNotes release];
[selectedNote release];
[tblNotes release];
[super dealloc];
}
It is because just as you stated that txtmesssagenotes was created using IB. And you never presented your ProductNote UIViewController instant productNote. So the txtmesssagenotes was never allocated and initialized properly.
Are you calling selectedNote: before your view has loaded?
Just because you created a view controller with a xib doesn't mean that it loads all your subviews immediately.
The xib is parsed and all your views are created the first time that the view controller's view property is asked for - this is usually just before it appears for the first time.
Until then, all the IBOutlet connections will be nil.
You need to store the text for your note as an NSString property of you view controller. Then, inside either viewDidLoad you need to call self.txtmesssagenotes.text=self.note; - viewDidLoad is automatically called just after your view is loaded so you are guaranteed to have all your IBOutlets set by then.
You shouldn't rely on your view objects to store the state of your app (in this case, the text of the note). If a low memory warning is received while your view controller isn't visible then all your view objects will be deleted - they will only be recreated when your view controller becomes visible again - if you are storing your note string inside a UITextView then it might no be there the next time you ask for it :) You should always store data in a property of your view controller (or somewhere else you control) and create your views using it.
I have a Navigation Controller with two Views. The First View is a ViewController with a TableView and the Second View is UIView with some UILabels and a UIWebView. When selecting a cell from the TableView, the Navigation Controller pushes to the Second View.
I've added a UIToolBar to Navigation Controller and some buttons.
My problem is when the Navigation Controller pushes to the Second View (UIView), my UIToolBar appears but without the buttons.
I would like to either show different buttons or remove the UIToolBar from the Second View.
I think I need to do something within the AppDelegate, I'm just not sure what and how.
Here is how my XIBS look:
MainWindow
- App Delegate
- Window
- Navigation Controller
-- Navigation Bar
-- ToolBar
-- View Controller
--- Bar Button Item
--- Navigation Item
---- Bar Back Item
ViewController
-TableView
DetailView
-View
--Web View
--Label
--Label
Code:
AppDelgate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController;
UIToolbar *toolBar;
}
#property (nonatomic, strong) IBOutlet UIWindow *window;
#property (nonatomic, strong) IBOutlet UINavigationController *navigationController;
#property (nonatomic, strong) IBOutlet UIToolbar *toolBar;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize window;
#synthesize navigationController;
#synthesize toolBar;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
#end
Any help would be appreciated. Thanks
You have to specify the toolBarItems for each View you load onto the stack. So,
-(void)viewDidLoad{
[self setToolbarItems:myArrayOfItems];
//do this in every view controller
//other code.
}
I cant get you clearly. i suggest this with the assumption. If you want add button in the top bar of the nextview means, you dont use toolbar for that. You can add button in the navigation bar itself.
you can add the button using addsubview method.
Initialize UIbutton using initwithframe. then add that as a subview of navigation controller.
[navigationcont addsubview:button];
i am having a tableview in which custom cells are loaded.Custom cell has a button on click of which a pickerview will open which will have options to choose from.
The problem is that modalViewController method is not working, it is giving the following error.
Selector *sel = [[Selector alloc]initWithNibName:#"Selector" bundle:nil];
[self PresentModalViewController:sel animated:YES];
error:property presentModalViewController not found on object of type CustomCell *...and selector is the pickerview controller class...the method is written in ibaction function in customcell.m file
How can v call other view from custom cell?
thanks
First, naming your class "Selector" is a horribly confusing idea. You should use something more descriptive, and something that is not already an obj-c keyword.
As for your problem, I think you should use a delegate to get a reference from your cell view to the controller. In your custom cell view class, do something like:
#property (nonatomic, assign) id delegate;
// implementation
#synthesize delegate = _delegate;
// in your cell... method
[self.delegate presentPicker];
Here, the delegate ivar would point back to your view controller. To set that up, find the place where you alloc your cell, and do
ACell *aCell = [ACell alloc] init];
aCell.delegate = self;
I've managed to read some values into a table view and display them in the Master View of a SplitViewController.
What I would like to do is to tap on a row of the Master View and display the details on the detailViewController but in a TableView.
When I tap on the row in the MasterView table, I can't seem to get the detail to populate the detailview TableView.
Any suggestions?
Thanks in advance.
Here's what you need to do:
Add an outlet to your master view controller which is a link to your detail view controller
Connect this outlet (either in Interface Builder or in code)
Add properties to your detail view for the values you're interested in displaying
in your implementation of tableView:didSelectRowAtIndexPath: retrieve the data for the selected row and set the corresponding properties in your detail view
Read this basic tutorial.
http://doronkatz.com/ipad-programming-tutorial-hello-world
Go through the sample code and see what you are doing wrong or missing
EDIT: its a tutorial on splitViewController.
may be this code is help for you
in table View .m file
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.dvController == nil){
DetailsViewController *viewControoler = [[DetailsViewController alloc]initWithNibName:#"detailsViewController" bundle:[NSBundle mainBundle]];
self.dvController = viewControoler;
[viewControoler release];
}
DocumentNavController *docObjNew = [appdelegate.noteArray objectAtIndex:indexPath.row];
[docObjNew hydrateDetailViewData];
//
dvcontroller.noteObj = docObjNew; //noteobj reference from in DetailsViewController.m file DocumentNavController *noteObj;
dvcontroller.currentindex = indexPath.row;
[self.navigationController pushViewController:self.dvController animated:YES];
self.dvController.title = [docObjNew noteTitle];
[self.dvController.noteTitelFiled setText:[docObjNew noteTitle]];
[self.dvController.notediscView setText:[docObjNew noteDisc]];
}
in table view .h file
#interface DocumentTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *documenttableView;
evernoteAppDelegate *appdelegate;
UINavigationController *navControll;
DetailsViewController *dvcontroller;
DocumentNavController *docNavContro;
//NSIndexPath *selectIndexPath;
}
#property (nonatomic, retain)DetailsViewController *dvController;
#property (nonatomic, retain)DocumentNavController *docNavContro;
#property (nonatomic, retain)UITableView *documenttableView;
in DetailsViewController.h file
UITextField *noteTitelFiled;
UITextView *notediscView;
DocumentNavController *noteObj;
#property (nonatomic, retain)IBOutlet UITextField *noteTitelFiled;
#property (nonatomic, retain)IBOutlet UITextView *notediscView;
#property (nonatomic, retain)DocumentNavController *noteObj;