Managing rows in view based application - iphone

I was able to manage rows(add, delete and reorder) of an uitableviewcell in navigation based application, but it is not working in a view based application. The edit button that created in navigation type application is making it possible to edit. Anyone knows what action method is called when the touch up inside of this navigation button occurs? What is happening on this method is that the provisions for adding, deleting and reordering rows coming up on this action, but i've not written any action method of this. SO is there any similar way in a view based application to do these things??

The UIViewController is having its setEditing:animated: method called by that nav bar button. If you're hooking up a regular button, your handler should call that method, and the method should look something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated]; // must be called first according to Apple docs
[table setEditing:editing animated:animated];
}
i.e. your controller will tell the table to also go into editing mode.

Related

UINavigationController not popping a view when back button pressed, but navigation bar animates

I'm creating a very simple sample app to show how preferences will work with a new application I'm working on. The process is very simple: create a UINavigationController in IB, assign it the View Controller that will be the root view controller, and push a new view controller onto the navigation controller's stack when didSelectRowAtIndexPath is called.
I've read as many related topics to this as I can find and never found a satisfactory answer.
The problem is that when the back button is pressed, the navigation controller animates back, but the view itself doesn't change back, meaning it's not getting popped.
This is the code I use to push the new view controller onto the stack. It's pretty standard. I added the NSLog to make sure it's only getting hit once.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section != 0)
return;
NSLog(#"didSelectRowAtIndexPath\n");
PrefsListTableViewController *prefsListTableViewController = [[PrefsListTableViewController alloc] initWithNibName:#"PrefsListTableViewController" bundle:nil];
[self.navigationController pushViewController:prefsListTableViewController animated:YES];
[prefsListTableViewController release];
}
To check if the viewWillDisappear call is hit on the second view, I added this to my PrefsListTableViewController:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(#"ptvc viewWillDisappear");
}
This never gets hit.
First, I was under the impression that you don't need to code anything to handle the back button since the UINavigationController should pop the stack for you automatically when you press the back button. I can show that with a sample app I have which does what I'm doing, but the sample is code only. The code is from Erica Sadun's iPhone Developer's Cookbook code, recipe 11-11. Her code is here:
https://github.com/erica/iphone-3.0-cookbook-/tree/master/C11-Tables/11-Disclosure%20Chevrons
If you look at her code, there are no .xib files, and everything is handled without the need to code the back button at all. The view controllers are pushed in code, and popped, seemingly, without code. I prefer to use .xib files and everything SHOULD be working the same way, but it's not.
Second, I put in NSLog statements to show that in fact, the root navigation controller has the two view controllers.
2011-11-18 11:14:36.355 TableViewTest[58011:207] didSelectRowAtIndexPath
2011-11-18 11:14:36.358 TableViewTest[58011:207] ptvc viewWillAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)
2011-11-18 11:14:36.717 TableViewTest[58011:207] ptvc viewDidAppear (
"<TableViewController: 0x8923d40>",
"<PrefsListTableViewController: 0x8927a20>"
)
So you can see that the view controllers are pushed and in the proper order.
That leads to one question: why isn't PrefsListTableViewController getting popped when the back button is pressed? If anyone needs any more info, please let me know.
Have you extended UINavigationController Class? I did and had the same issue. Apparently, according to apple docs, your not supposed to extend that class...

Why isn't viewWillDisappear or viewDidAppear being called?

I have a UINavigationController with a UITableView as my main menu. User clicks on a cell and a new view is pushed on the stack. In one case I push another UITableView that needs a toolbar. So on that 2nd tableView's init I setup the self.toolbarItems property with the correct items. But then I need to call [self.navigationController setToolbarHidden:NO animated:YES]; So it makes sense to call this in the viewDidAppear or viewWillAppear method. But I put it in those methods and find out (Also via NSLog) that they never get called. The same goes for hiding it in viewWillDisappear or viewDidDisappear. Why don't these methods get called? Where should I be doing this hiding/showing of the toolbar then?
I have noticed behavior where if a parent controller (like UINavigationController or UITabBarController) never get's viewWill/DidAppear called on it, it won't call it on the child controllers either. So make sure that in the code where you create the parent controller, you call viewWillAppear, show it, then call viewDidAppear. Then it should make those calls on it's child controllers as is appropriate.
Double check the parent controller is having those methods called, and call them yourself if they are not.
Yes Its true
you can do this by first write this code in
- (void)viewDidLoad {
self.navigationController.delegate = self;
}
And then write the code which you want to write in viewWillAppear
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[self class]]) {
//write your code here
}
}
Although you solved your problem, in case someone comes along in the future another problem could have been that you forgot the animated: argument to either method - that is to say, the format of the method needs to look like:
- (void) viewWillAppear:(BOOL)animated
i noticed the same issue in iOS7. When i'm using both tab bar (2 buttons A, B) and navigation controller.
A has two views. One with tableview and second displays data according to the selection from the table view.
B has is the only view.
Button which is refer to another separate view D, placed in both tab bar views (A & B) and in both views of A.
Problem arises when i click the button from tab item B, viewWillAppear and viewDidLoad not called.
So i solved this issue by presentModalViewController:animated: and to come back i used dismissModalViewControllerAnimated:, just when i go to view D from tab item B.

How to call method automatically when switching views in TabBar app with a single ViewController

My application uses UITabBarController with 4 tabs. Each tab will have a UIWebView along with other types of objects. When the app launches I need to call the method for this first webView to retrieve my web content.
I have this method in my viewdidLoad:
[self performSelectorInBackground:#selector(getAdvisory) withObject:nil];
The web content on the first tab works fine. I'm just at a loss to get my other tabs to load up. I think I would use a switch or if statement but I do not know how to tell which view is loaded.
I need to do the same for the rest of the tabs. The app has a single view controller.
When setting an action using a button everything works fine. I just do not know how to call the method when a different view (tapping tabs) loads.
Also when retrieving data from the network, what are the best methods to use to not tie up the main thread? I have read where NSOperation would be used in this scenario. Is this correct? If so how would I go about doing this?
Thanks in advance.
I was looking for a solution to this problem and there is a better method.
Simply override the viewDidAppear method and insert the code you want to execute when the view does appear!
As an example the following code will call myMethod.
-(void) viewDidAppear:(BOOL)animated {
[self MyMethod];
}
maybe by using a user delegate of your UITabBar and the method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Sent to the delegate when the user selects a tab bar item.
See reference.

How to recreate UIViewController stack?

I'm writing a 'load' feature for my current iPhone app.
Practically what I want to do is get the user back where s/he left off. To do so I want to recreate all the UIViewControllers appeared before.
My problem is the UIViewControllers won't appear if I simply call [[self navigationController] pushViewController:newController animated:YES];.
The same code works fine when invoked as an event handler, like after touching a button. But if I do same without an even (for ex. in viewDidLoad) the new view controller's loadView method won't get called.
My actual code (with some pseudo elements):
- (void)viewDidLoad {
[super viewDidLoad];
if (loading)
[self onSomeEvent];
}
- (void)onSomeEvent {
UIViewController *newController = //init....;
[[self navigationController] pushViewController:newController animated:YES];
[newController release];
}
I guess viewDidLoad is not the right place to do such a call, but then what is?
I'm not sure that spreading your "load" feature all accross your controllers is the best way to achieve it.
I would rather put it in the init of your application, in the applicationDidFinishLauching part. Then you have a centralized place where you restore the previous state (easier to handle IMHO).
Let's suppose you want to implement some more advanced restore feature like:
displaying a splash UIView with an activity indicator to indicate the user that you're restoring previous state
restore the stack of your controllers in the navigation controller
remove the splash
it's easier to have all this code in the applicationDidFinishLauching : it's all managed at one point.
Morever, when restoring the old state to your navigation controller, you can avoid using the transitions and use animated:NO instead of YES when pushing your controllers. It will be easier to handle from your perspective and the restore time may be decreased if you remove time needed to achieve transitions.

How to automatically call a method after popping a view controller off the stack on the iPhone

I need to update the parent view on an iPhone after popping a child view off the navigation stack. How can I setup the parent view to be notified or receive an automatic method call when the child is popped off the stack and the parent becomes visible again?
The user enters data on the child page that I want to display on the parent page after the user is done and pops the view.
Thanks for you help!
I just resolved this self same problem - and the answers above are almost correct, they just forgot about setting the delegate.
I have a root view controller that displays the size of a list, calls a child view controller that may alter the size of a list, and must update the size upon return.
When I create my parent view (SettingsView below), and add it as the root view of a UINavigationController, I make sure to set the UINavigationController's delegate before I display the view - that's the key part:
SettingsView *sv = [[SettingsView alloc] initWithNibName:#"SettingsView" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sv];
[nc setDelegate:sv];
In the parent view, implement the UINavigationControllerDelegate protocol:
#interface SettingsView : UIViewController <UINavigationControllerDelegate>
and provide the willShowViewController method:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// Your code to update the parent view
}
This is called after the child view is dismissed, and before the parent view is redisplayed.
I had the need to do something like this as well. In the ViewController that owned my UINavigationController, I had to implement willShowViewController, like this:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
}
That method is called whenever the UINavigationController changes views. If I'm understanding your question correctly, I think this should do what you want.
I think there is some confusion here. UIViews are not pushed to and popped from the UINavigationController's stack. What is being pushed and popped is UIViewControllers, which in turn handle one or (more often) several views each.
Fortunately, the UIViewController has these methods:
-(void) viewWillAppear:(BOOL)animated;
-(void) viewDidAppear:(BOOL)animated;
-(void) viewWillDisappear:(BOOL)animated;
-(void) viewDidDisappear:(BOOL)animated;
These are called whenever the view is about to (dis)appear, or has just (dis)appeared. I works with tab bars, modal views and navigation controllers. (And it's a good idea to make use of these when you implement custom controllers.)
So in your case, if I understand correctly, you simply have to override the viewWillAppear: or viewDidAppear: method on what you call the "parent page" (which is presumably handled by a UIViewController) and put in code to update the appearance of the page to reflect the data just entered.
(If I remember correctly, you must make sure that the UINavigationController gets a viewWill/DidAppear: message when it is first displayed, in order for these messages to later be sent to its child controllers. If you set this up with a template or in IB you probably don't have to worry about it.)
Felixyz answer did the trick for me. Calling the view will appear method will run the code in it every time the view appears. Different from view did load, which runs its code only when the view is first loaded. So your parent view would not update itself if a child view altered the info displayed in the parent, and was then popped off the sack. But if the parents calls view will appear, the code gets ran every time the view shows back up.
Make sure to call the super method at the same time. Proper implementation would look like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(#"View Appearing");
}
If you need to notify one controller to another you may use delegation pattern as described here (see 2nd answer).
Unfortunately there is no automatic notification(AFAIK) for exact task as you described.
To meet your needs you may send message to delegate (i.e. to your parent controller) in viewWillDisappear function of your child controller.